Reputation: 667
Im binding data from database to webgrid, i want to keep an actionlink edit in every row of webgrid, tried the below one but getting error as Object reference not set to an instance of an object
near the below line, my model name is User
foreach(var item in Model)
im also posting the whole code
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" })
}
Upvotes: 0
Views: 3046
Reputation: 1039368
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.
Upvotes: 4