ViewBag返回NullReferenceException

问题描述 投票:1回答:1

我已经从数据库中检索了数据列表。数据列表在控制器类中工作正常,并且确实具有值,但是当我将其与ViewBag.List绑定并尝试将其呈现为NullReferenceException时,将其呈现。我不知道为什么会给这个例外。ControllerClass

public ActionResult Index()
{ 
    var SomeList = Database.somelamdaexp.ToList();
    ViewBag.List = SomeList;
    return View();
}

查看

@foreach(var item in ViewBag.List)
{
    <tr>
        <td>@item.Field1<td>
        <td>@item.Field2</td>
    </tr>
}
c# asp.net-mvc nullreferenceexception
1个回答
0
投票

您的代码正在按预期工作,see the fiddle

您已验证模型中确实存在Field1Field2吗?

© www.soinside.com 2019 - 2024. All rights reserved.