potatomen D
potatomen D

Reputation: 1

Can't add data into ViewModel DbSet

public ActionResult MojeTeme() 
{
    var db = new Palindrom.Models.Baza1Entities();
    var db1 = new Palindrom.Models.Entities();

    var db2 = new Palindrom.VjuModeli.ProfilKorisnika();

    var naslovi = db.PalindromBaza.Select(m => new Palindrom.VjuModeli.TemeKorisnika { Korisnik = m.Korisnik, ID = m.ID, Naslov = m.Naslov });

    foreach (var red in naslovi)
    {
        if (db2.teme != null)
            db2.teme.Add(red);
    }

    return View(db2);
}

I have a problem understanding why this code isn't working.

I have two models that I need some data from that I want to put into a single view model:

public class ProfilKorisnika
{
    public DbSet<TemeKorisnika> teme { get; set; }
    public  DbSet<PKorisnika> profil { get; set; }
}

I want to focus on just one of these DbSets as I hope it's just a copy and paste afterwards.

DbSet<TemeKorisnika> should have no data before the db2.teme.Add(red) line.

I made a variable naslovi to hold the three columns that I need for that DbSet<TemeKorisnika> and I wanted to use a foreach loop to fill the db2.teme. Problem is, when I try to load the View, I'm greeted with a NullReferenceException and the Debugger directs me to this line of code: db2.teme.Add(red).

I'm certain that the red variable isn't null, nor is the naslovi variable.

Through some research, I have been led to believe that db2.teme isn't initialized, as a beginner I'm not completely sure I know what that means in this situation, how can it not be initialized if it's a part of the initialized class ProfilKorisnika that's instantiated in the db2 object, am I missing something?

I have been spinning around this way of adding the data for a while and haven't tried anything that doesn't resemble this.

Upvotes: 0

Views: 27

Answers (1)

potatomen D
potatomen D

Reputation: 1

I changed the bulk of the code to this and it works:

var naslovi = db.PalindromBaza.Where(m => m.Korisnik == User.Identity.Name);
       
        var ajdijevi = db.PalindromBaza.Where(m => m.Korisnik == User.Identity.Name);

        var profil = db1.AspNetUsers.Select(m => new Palindrom.VjuModeli.PKorisnika {OpisPr = m.OpisProfila,NazivPrSl=m.NazivProfilneSlike ,Korisnik = m.UserName});

        List<Palindrom.VjuModeli.VjuModZaProfil> profilov = new List<Palindrom.VjuModeli.VjuModZaProfil>();
        int i = -1;

        try { 
            ajdijevi.First(); 
            foreach (var red in naslovi)
            {
                i++;
                profilov.Add(new VjuModeli.VjuModZaProfil
                {
                    ID = Convert.ToInt32(ajdijevi.ToArray()[i].ID),
                    Korisnik = profil.First(m => m.Korisnik == User.Identity.Name).Korisnik.ToString(),
                    Naslov = naslovi.ToArray()[i].Naslov.ToString(),
                    OpisPr = profil.FirstOrDefault(m => m.Korisnik == User.Identity.Name).OpisPr.ToString(),
                    NazivPrSl = "img.png"                
                }
               );
            }
        }

        catch { 
            profilov.Add(new VjuModeli.VjuModZaProfil
            {
                Naslov = "nslv",
                ID = 0,
                Korisnik = profil.First(m => m.Korisnik == User.Identity.Name).Korisnik.ToString(),
                OpisPr = profil.FirstOrDefault(m => m.Korisnik == User.Identity.Name).OpisPr.ToString(),
                NazivPrSl = "img.png"
            }
            );
        }
        return View(profilov);

I also modified the ViewModel to this:

public class VjuModZaProfil
{
    public int ID { get; set; }
    public string Korisnik { get; set; }
    public string Naslov { get; set; }
    public string OpisPr { get; set; }
    public string NazivPrSl { get; set; }
}

Hopefully this helps someone, if there is something that's nonsensical to anyone about this code, I'd like to clear it up.

Upvotes: 0

Related Questions