MVC 3 Webgrid Column

user415394 picture user415394 · Jan 23, 2011 · Viewed 15.6k times · Source

I'm working on a MVC 3 webgrid at the moment, in one of the columns I wish to have a button, I've achieved this when putting the following code in the view.

@grid.GetHtml(columns:
            grid.Columns(
            grid.Column("ID", "id", canSort: true),
            grid.Column("Surname", "surname", canSort: true),
            grid.Column("Forenames", "forename", canSort: true),
            grid.Column(format: @<input type="button" value="View"/>)),
            headerStyle: "header",
            alternatingRowStyle: "alt",
            htmlAttributes: new { id = "DataTable" }
            )

However I wish to create the grid server side for the purpose of paging, but when I put the code below in the action I get a error for for the button column.

var htmlString = grid.GetHtml(tableStyle: "webGrid",
                                          headerStyle: "header",
                                          alternatingRowStyle: "alt",
                                          htmlAttributes: new { id = "DataTable" },
                                          columns: grid.Columns(
                                                grid.Column("ID", "id", canSort: true),
                                                grid.Column("Surname", "surname", canSort: true),
                                                grid.Column("Forenames", "forename", canSort: true),      
                                                grid.Column(format: @<input type='button' value='View'/>)                                                                          
                                           ));

The first error is "Keyword, identifier, or string expected after verbatim specifier: @".

Am I using the incorrect format on the button column?

Answer

Stuart Davies picture Stuart Davies · Jan 25, 2011

It looks like you're attempting to use Razor syntax in your code-behind. Try something like this, using a lambda expression...

gridColumn.Format = (item) =>
{
    return new HtmlString("<input type='button' value='View'/>");
}