Display encoded html with razor

前端 未结 6 1714
心在旅途
心在旅途 2020-12-02 10:13

I store encoded HTML in the database.

The only way i could display it correctly is :

@MvcHtmlString.Create(HttpU
6条回答
  •  天涯浪人
    2020-12-02 11:07

    I just got another case to display backslash \ with Razor and Java Script.

    My @Model.AreaName looks like Name1\Name2\Name3 so when I display it all backslashes are gone and I see Name1Name2Name3

    I found solution to fix it:

    var areafullName =  JSON.parse("@Html.Raw(HttpUtility.JavaScriptStringEncode(JsonConvert.SerializeObject(Model.AreaName)))");
    

    Don't forget to add @using Newtonsoft.Json on top of chtml page.

提交回复
热议问题