ASPX View MVC3 WebGrid format column

     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">Select<span>

and the html shown will be just Select

Last updated:11/20/2014 6:10:33 AM

1 Answers

Anonymous User
Anonymous User

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