ASPX查看MVC3 WebGrid格式列

时间:2012-05-29 15:03:03

标签: asp.net asp.net-mvc-3 view webgrid

<%{
     WebGrid studentGrid = new WebGrid(rowsPerPage: StudentController.PageSize);
     studentGrid.Bind(Model.Students, autoSortAndPage: false, rowCount: Model.RowCount);
     studentGrid.PageIndex = Model.CurrentPage;
}%>

<%=studentGrid.GetHtml(columns: new WebGridColumn[]{
    studentGrid.Column("StudentId", "Id"),
    studentGrid.Column("Name", "Name"),
 })%>

不幸的是,我被迫在我的MVC3项目中使用aspx视图。

我希望有一个列,根据绑定到网格的列表项的某些条件显示文本“select”或“remove”。

sysntax如何做到

我必须得到像

这样的渲染html
<span class="1" id=item.id>Select<span>

,显示的html只是选择

1 个答案:

答案 0 :(得分:2)

您绝对可以使用format进行操作,只需要在C#中手工编写HTML:

<%

    var list = new[]
                    {
                        new { StudentId = 1, Name = "Name1", Cond = true },
                        new { StudentId = 2, Name = "Name3", Cond = false },
                        new { StudentId = 2, Name = "Name3", Cond = true },
                    };
    WebGrid studentGrid = new WebGrid();
    studentGrid.Bind(list, autoSortAndPage: false, rowCount: 3); 

%>
<%= studentGrid.GetHtml(columns: 
    new WebGridColumn[]
        {
            studentGrid.Column("StudentId", "Id"),
            studentGrid.Column("Name", "Name"),
            studentGrid.Column(header: "Action",  
                format: item =>
                {
                    string span = "<span class=\"1\" id=\"{0}\">{1}<span>";
                    string action = item.Cond ? "Select" : "Remove";
                    return Html.Raw(string.Format(span, item.StudentId, action));
                })
        })
%>

一般支持在lambda中使用aspx模板systax(例如<% %>)(请参阅此Telerik demo),但因为WebGrid比Telrik工作得不明显,所以它不起作用。< / p>

似乎构建WebGrid的方式只支持format参数中的剃刀模板...

相关问题