问题
我正在尝试在 MVC 中实现 HTML5/jQuery 模型,但遇到了一个我以前从未遇到过的问题;它似乎使用 Html.TextBoxFor、Html.PasswordFor 和其他所有自动编码传递的任何 HTML 属性。
示例
以下面一行为例:
@Html.TextBoxFor(m => m.Pin, new { data_regexp="<\\d{4}>" })
此代码将用于验证信用卡上的 PIN 码,我需要使用数据正则表达式模式来传递适当的正则表达式。我希望输出如下:
<input data-regexp="<\d{4}>" id="Pin" type="text" />
相反,它似乎是从内置扩展中调用 HtmlAttributeEncode ( http://msdn.microsoft.com/en-us/library/wdek0zbf.aspx ),导致以下编码输出:
<input data-regexp="<\d{4}>" id="Pin" type="text" />
所需的解决方案说明
除了编写自己的扩展程序之外,我还有其他选择吗?使用其他形式的验证不是一种选择,这个例子只是为了说明手头的问题 - 我有几个其他例子,我需要打印原始 HTML,而不是编码版本。
编辑: 使用 Html.Raw 或 HttpUtility.Decode 没有影响。编码发生在任何这些应用之后。
最佳答案
想通了。我添加了以下类(class):
namespace MyProjectName.Extensions
{
public class HtmlAttributeNoEncoding : HttpEncoder
{
protected override void HtmlAttributeEncode(string value, TextWriter output)
{
output.Write(value);
}
}
}
并修改了我的网络配置:
<system.web>
...
<httpRuntime targetFramework="4.5" encoderType="MyProjectName.Extensions.HtmlAttributeNoEncoding"/>
...
</system.web>
关于c# - MVC 4 TextBoxFor 和其他自动调用 HttpUtility.HtmlAttributeEncode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642476/