MVC 3 Webgrid Column

15,661

Solution 1

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'/>");
}

Solution 2

you can try <text> tag for razor like this;

grid.Column(format: @<text><input type='button' value='View'/></text>)   
Share:
15,661
user415394
Author by

user415394

Updated on June 25, 2022

Comments

  • user415394
    user415394 about 2 years

    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?

  • user415394
    user415394 over 13 years
    Nearly there, was grid.Column(format: (item) => {return new HtmlString("<input type='button' value='Vieww'/>");}) Thanks.