ASPX View MVC3 WebGrid format column

試著忘記壹切 提交于 2019-12-11 07:41:45

问题


<%{
     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"),
 })%>

Unfortunatly i am forced to use aspx view in my MVC3 project.

I want to have a column that shows a text "select" or "remove" based on some condition of the list item that is bound to the grid.

How is the sysntax to do that

I have to get render html like

<span class="1" id=item.id>Select<span>

and the html shown will be just Select


回答1:


You can definitely do it with format you just need to handcraft the HTML in C#:

<%

    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));
                })
        })
%>

Using the aspx template systax (eg. <% %>) inside a lambda is supported in general (see this Telerik demo) but because the WebGrid working diffidently than Telrik it's not working.

It seems the way is WebGrid built only supports razor templates inside the format argument...



来源:https://stackoverflow.com/questions/10801743/aspx-view-mvc3-webgrid-format-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!