Shawn Mclean
Shawn Mclean

Reputation: 57479

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.

Upvotes: 1

Views: 3381

Answers (2)

daanl
daanl

Reputation: 44

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

Upvotes: 0

LukLed
LukLed

Reputation: 31882

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.

Upvotes: 3

Related Questions