开发者

Do not html encode string in asp.net razor

开发者 https://www.devze.com 2023-02-11 06:58 出处:网络
I\'m following the code on here in razor syntax. I end up with this: I tried this: @{Html.Grid(\"basic\")

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

0

精彩评论

暂无评论...
验证码 换一张
取 消