Display raw HTML using WebGrid in ASP.NET MVC 3

I have a grid like this:

WebGrid grid = new WebGrid(source: Model, rowsPerPage: 5, ajaxUpdateContainerId: "GridContainer"); 

Now I want to display the "MyContent" column as raw HTML . What should I do?

 <div id="GridContainer"> @grid.GetHtml(columns: grid.Columns( grid.Column( columnName: "MyContent", //Format: What should I put here? ) ) ) </div> 
+4
source share
2 answers

Using

 <div id="GridContainer"> @grid.GetHtml(columns: grid.Columns( grid.Column( columnName: "MyContent", format: (item) => { var links = Html.ActionLink("Edit", "Edit", new {id = item.PrimaryKey}) + " | " + Html.ActionLink("Details","Details", new { id = item.PrimaryKey}) + " | "+ Html.ActionLink("Delete", "Delete", new { id = item.PrimaryKey}); return Html.Raw(links); } ) ) ) 

which is displayed as

 <td> <a href="/Home/Edit/5">Edit</a> | <a href="/Home/Details/5">Details</a> | <a href="/Home/Delete/5">Delete</a> </td> 
+5
source

I think it’s much more convenient to use annotations in the model.

 [GridColumn(Title="&nbsp;", SanitizeEnabled=false,EncodeEnabled=false)] public IHtmlString RateLink { get{ return new HtmlString("<a href=\"" + BaseUrl + Id + "\" target=\"_blank\">Open</a>"); } } 

If you only use annotations that you only need for html:

 @Html.Grid(Model).AutoGenerateColumns() 
0
source

All Articles