MVC 4 TextBoxFor and others automatically calling
HttpUtility.HtmlAttributeEncode
Problem
I'm trying to implement a HTML5/jQuery mock-up in MVC and am running into
an issue I've never encountered before; it appears using Html.TextBoxFor,
Html.PasswordFor and others all automatically encode any HTML attributes
that are passed.
Example
Take the following line for example:
@Html.TextBoxFor(m => m.Pin, new { data_regexp="<\\d{4}>" })
This code would be used to validate a PIN number on a credit card and I
need to use the data-regexp pattern to pass the appropriate regular
expression. I would expect the output to be the following:
<input data-regexp="<\d{4}>" id="Pin" type="text" />
Instead, it appears to be calling HtmlAttributeEncode
(http://msdn.microsoft.com/en-us/library/wdek0zbf.aspx) from within the
built in extension, resulting in the following encoded output:
<input data-regexp="<\d{4}>" id="Pin" type="text" />
Desired Solution Note
Do I have any other option than to write my own extensions? Using other
forms of validation isn't an option and this example is just to show the
issue at hand - I have several other examples where I need the raw HTML
printed, not an encoded version.
Edit: Using Html.Raw or HttpUtility.Decode have no impact. The encoding
occurs after any of these are applied.
No comments:
Post a Comment