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.
I changed the bulk of the code to this and it works:
I also modified the ViewModel to this:
Hopefully this helps someone, if there is something that's nonsensical to anyone about this code, I'd like to clear it up.