MVC3 TextBoxFor有连接codeD文字 [英] MVC3 TextBoxFor with encoded text
问题描述
有没有办法使用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
为
将导致
<脚本>警报();'< / SCRIPT>
TextBoxFor不支持的话,你有2个选项
1。德code显示前
@ {
Model.Description = System.Web.HttpUtility.HtmlDe code(Model.Description);
}
@ Html.TextBoxFor(型号=> 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:
<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))
?
You have to html-decode your string.
Use the System.Web.HttpUtility.HtmlDecode
for that.
System.Web.HttpUtility.HtmlDecode("<script>alert();'</script>")
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屋!