0

我将数据从数据库绑定到 webgrid,我想在 webgrid 的每一行中保留一个 actionlink 编辑,尝试了下面的一个但Object reference not set to an instance of an object在下面的行附近出现错误,我的模型名称是 User

foreach(var item in Model)

我还发布了整个代码

var grid = new WebGrid(source: MvcPopupGrid.Models.User.Users, rowsPerPage: 5);
@grid.GetHtml(
    tableStyle: "grid", headerStyle: "gridhead", footerStyle: "paging", rowStyle: "td-dark", alternatingRowStyle: "td-light",
        columns:
             grid.Columns(
             grid.Column(header: "Id", format: @<text><label id="lblId" title="@item.Id">@item.Id</label></text>),
             grid.Column(header: "Name", format: @<text><label id="lblName" title="@item.Name">@item.Name</label></text>),
             grid.Column(header: "College", format: @<text><label id="lblCollege" title="@item.College">@item.College</label></text>),
             grid.Column(header: "PassedOut", format: @<text><label id="lblPassedOut" title="@item.PassedOut">@item.PassedOut</label></text>),
             grid.Column(header: "Mobile", format: @<text><label id="lblMobile" title="@item.Mobile">@item.Mobile</label></text>)))
foreach(var item in Model)
{
    @item.Id
    @item.Name
    @item.College
    @item.PassedOut
    @item.Mobile
    @Html.ActionLink("Edit", "UserEdit", new { Id = "@item.Id" }, new { @class = "abookModal", title = "Edit User" })
}
4

1 回答 1

4

Your model is null. I guess that your view is strongly typed to some collection:

@model IEnumerable<SomeType>
@foreach (var item in Model)
{
    ...
}

except that inside the controller action that rendered this view you didn't pass any model to the view or you passed null. So make sure that this doesn't happen:

public ActionResult SomeAction()
{
    IEnumerable<SomeType> model = ... fetch the collection from somewhere and make sure this collection is not null
    return View(model);
}

Another thing I am noticing is that you are pointing your WebGrid source to some MvcPopupGrid.Models.User.Users property:

var grid = new WebGrid(source: MvcPopupGrid.Models.User.Users, rowsPerPage: 5);

You should also make sure that this property doesn't return null.

于 2012-08-27T11:47:27.253 回答