I have retrived a data list from database. The datalist works fine in controller class and does indeed have values in it but when I bind it with ViewBag.List
and try to render it in view it give NullReferenceException
. I have no idea why would it gives that exception.
ControllerClass
public ActionResult Index()
{
var SomeList = Database.somelamdaexp.ToList();
ViewBag.List = SomeList;
return View();
}
View
@foreach(var item in ViewBag.List)
{
<tr>
<td>@item.Field1<td>
<td>@item.Field2</td>
</tr>
}