Asp.Net MVC3 Web网格

时间:2013-02-10 22:44:57

标签: asp.net-mvc-3

为什么这个webgrid迭代网格中的每一行?

@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>
}

我的意思是:例如,对于两个数据,它会呈现4个数据,每个数据两次!

2 个答案:

答案 0 :(得分:0)

您不需要通过模型foreachGetHtml()将为您做好一切。

当您将Model传递给WebGrid()构造函数时,网格现在知道在您调用GetHtml()时必须呈现的数据,因此您无需执行任何其他操作。< / p>

这应该这样做:

@{
    var grid = new WebGrid(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=item.Id},null)),
        grid.Column("",header:"",format:(item)=>Html.ActionLink("Delete","Delete","Blog",new{postId=item.Id},null)),
        grid.Column("",header:"",format:(item)=>Html.ActionLink("Edit","Edit","Blog",new {postId=item.Id},null)), })
     </div>
}

答案 1 :(得分:0)

我不是百分百肯定,因为我不使用WebGrid,但您确定需要@foreach吗? @grid.GetHtml()不会返回所有记录吗?