MVC3 TextBoxFor with encoded text

断了今生、忘了曾经 提交于 2019-12-10 12:59:58

问题


Is there a way to use TextBoxFor helper with encoded text?

for example: When using the following helper of MVC3 With Razor view engine :

@Html.TextBoxFor(model => model.Description)

and the value of model.Description is encoded, for example:

 <script>alert();'</script>

the result is text box with the the encoded string, when the wanted result is text box with the decoded string:

 <script>alert();'</script>

Is there a way to use the MVC TextBoxFor with encoded string instead of using

@Html.TextBox("Description", Server.HtmlDecode(Model.Description))

?


回答1:


You have to html-decode your string.

Use the System.Web.HttpUtility.HtmlDecode for that.

System.Web.HttpUtility.HtmlDecode("&lt;script&gt;alert();&#39;&lt;/script&gt;")

will result in

<script>alert();'</script>

TextBoxFor does not support that so, you have 2 options

1. Decode before display

    @{
        Model.Description = System.Web.HttpUtility.HtmlDecode(Model.Description);
     }
    @Html.TextBoxFor(model => model.Description)

2. Use @Html.TextBox for this

    @Html.TextBox("Description", System.Web.HttpUtility.HtmlDecode(Model.Description))

hope this helps



来源:https://stackoverflow.com/questions/8402230/mvc3-textboxfor-with-encoded-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!