asp.net-mvc – 带编码文本的MVC3 TextBoxFor

有没有办法使用TextBoxFor帮助程序与编码文本?

例如:
当使用以下MVC3帮助器和Razor视图引擎时:

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

和model.Description的值被编码,例如:

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

结果是带有编码字符串的文本框,
当想要的结果是带有解码字符串的文本框时:

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

有没有办法使用MVC TextBoxFor编码字符串而不是使用

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

解决方法

你必须对你的字符串进行html解码.

使用System.Web.HttpUtility.HtmlDecode.

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

会导致

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

TextBoxFor不支持,因此您有2个选项

1.显示前解码

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

2.使用@ Html.TextBox

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

希望这可以帮助

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...