How to add Html inside ASP.NET MVC 3 WebGrid Column Name (Header)

11,875

There is a method to change html generated by an MVC Helper, but it's not very comfortable. In general you can write this command:

@( new HtmlString( Component.HelperMethod().ToHtmlString().Replace("[replacetag]", "My Content")) )

Here is an example using a grid component:

<div id="grid">
    @(new HtmlString(
        grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "header",
        rowStyle: "row",
        footerStyle: "footer",
        alternatingRowStyle: "altRow",
        columns: grid.Columns(
                     grid.Column("Name", "[replacethis]"),
                     grid.Column("Surname", "Surname"),
                     grid.Column("Email", "Sender Email"),
                     grid.Column("Id", "", format: (item) => item.GetSelectLink("Select"), canSort: false))
).ToHtmlString().Replace("[replacethis]", "<b>Name</b>")
)
)    
</div>
Share:
11,875

Related videos on Youtube

user983443
Author by

user983443

Updated on June 04, 2022

Comments

  • user983443
    user983443 almost 2 years

    I am unable to add html inside WebGrid column name(at header)? When I add html inside column name then webgrid encodes that. Is this is not possible to add html inside WebGrid column name?

  • mhichwa
    mhichwa about 11 years
    @Max YES!! I have been trying to do this for days (ahem.. weeks). Thanks :-)
  • mostafakvd
    mostafakvd over 6 years
    Very nice solution thank you. I almost trying to find the solution for an hour.