Do not html encode string in asp.net razor
I'm following the code on here in razor syntax. I end up with this:
I tried this:
@{Html.Grid("basic")
.setCaption("Basic Grid")
.addColumn(new Column("JobId")
.setLabel("Id"))
.addColumn(new Column("Title"))
.addColumn(new Column("CreatedDate"))
.setUrl(Url.Action("Jobs"))
.setAutoWidth(true)
.setRowNum(10)
.setRowList(new int[]{10,15,20,50})
.setViewRecords(true)
.setPager("pager");}
开发者_开发百科and its displays nothing. I had it starting with just @
and it encoded the data.
Try:
@(new MvcHtmlString(Html.Grid("basic")
.setCaption("Basic Grid")
.addColumn(new Column("JobId")
.setLabel("Id"))
.addColumn(new Column("Title"))
.addColumn(new Column("CreatedDate"))
.setUrl(Url.Action("Jobs"))
.setAutoWidth(true)
.setRowNum(10)
.setRowList(new int[]{10,15,20,50})
.setViewRecords(true)
.setPager("pager").ToString())
Grid should return MvcHtmlString
(or just IHtmlString
) if you want it not to be encoded. The best solution is to write extension method called ToMvcHtmlString()
, that returns proper value. Then you would just use Html.Grid().ToMvcHtmlString()
. It is better than creating objects inside of view.
Try this:
@{ var grid = Html.Grid("basic")
.setCaption("Basic Grid")
.addColumn(new Column("JobId")
.setLabel("Id"))
.addColumn(new Column("Title"))
.addColumn(new Column("CreatedDate"))
.setUrl(Url.Action("Jobs"))
.setAutoWidth(true)
.setRowNum(10)
.setRowList(new int[]{10,15,20,50})
.setViewRecords(true)
.setPager("pager");
}
Html.Raw(grid.ToString());
Better is that grid.ToString() returns an IHtmlString so you don't need to Html.Raw it
精彩评论