why this webgrid iterate every row in grid ??
@model IEnumerable<Blog.Domain.Model.BlogPost>
@{
ViewBag.Title = "Posts";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
@Html.ActionLink("Add Post", "Add", "Blog")
@{
var grid = new WebGrid(Model);
}
@foreach (var items in Model)
{
<div id="grid">
@grid.GetHtml(columns: new[] {grid.Column("Title",header:""),
grid.Column("",header:"",format:(item)=>Html.ActionLink("More Details","Details","Blog",new{postId=items.Id},null)),
grid.Column("",header:"",format:(item)=>Html.ActionLink("Delete","Delete","Blog",new{postId=items.Id},null)),
grid.Column("",header:"",format:(item)=>Html.ActionLink("Edit","Edit","Blog",new {postId=items.Id},null)), })
</div>
}
my mean : for example for two data it renders 4 data , twice for every one !
You don’t need to
foreachthrough the model.GetHtml()will do everything for you.When you pass your
Modelinto theWebGrid()constructor the grid now knows what data it has to render when you callGetHtml()so you don’t need to do anything else.This should do it: