Witam, generuje sobie klasę w modelu Kontakty:
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Miasto { get; set; }
W kontrolerze tworzę sobie instancję, oraz zapełniam listę<Kontakty> do wysłania:
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
var k = new List<Kontakty> { new Kontakty { Imie = "Jan", Nazwisko = "Nowak", Miasto = "Warszawa" } };
ViewBag.Kontakty = k;
return View();
}
Następnie w Widoku Index.cshtml używam foreache:
<div>
@{
foreach (var kontakt in ViewBag.Kontakty)
{
<p>
@kontakt.Imie, @kontakt.Nazwisko, @kontakt.Miasto
</p>
}
}
</div>
Podczas uruchomienia dostaję error:
An exception of type 'System.NullReferenceException' occurred in App_Web_3gwpb12y.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Czy ktoś miałby pomysł?
Z góry dziękuję.
Pozdrawiam.