MVC3 TextBoxFor有连接codeD文字 [英] MVC3 TextBoxFor with encoded text

查看:91
本文介绍了MVC3 TextBoxFor有连接codeD文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用TextBoxFor帮手带连接codeD文字?

办法

例如:
当使用MVC3以下佣工的Razor视图引擎:

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

和model.Description的值为en codeD,例如:

 &放大器; LT;脚本&放大器; GT;警报();&安培;#39;&放大器; LT; / SCRIPT&放大器; GT;

的结果与该EN codeD字符串文本框中,
当通缉的结果是与德codeD字符串文本框:

 <脚本>警报();'< / SCRIPT>

是否有与使用MVC TextBoxFor EN codeD字符串,而不是使用方法

  @ Html.TextBox(说明,Server.HtmlDe code(Model.Description))


解决方案

您必须HTML的德code你的字符串。

使用 System.Web.HttpUtility.HtmlDe code

<$p$p><$c$c>System.Web.HttpUtility.HtmlDe$c$c(\"&lt;script&gt;alert();&#39;&lt;/script&gt;\")

将导致

 &LT;脚本&GT;警报();'&LT; / SCRIPT&GT;

TextBoxFor不支持的话,你有2个选项

1。德code显示前

  @ {
        Model.Description = System.Web.HttpUtility.HtmlDe code(Model.Description);
     }
    @ Html.TextBoxFor(型号=&GT; model.Description)

2。使用@ Html.TextBox这个

  @ Html.TextBox(说明,System.Web.HttpUtility.HtmlDe code(Model.Description))

希望这有助于

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:

 &lt;script&gt;alert();&#39;&lt;/script&gt;

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))

?

解决方案

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

这篇关于MVC3 TextBoxFor有连接codeD文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆