How to get textbox helper to display valid HTML markup for HTML 4.01 strict?
I have the following code in my pag开发者_JAVA技巧e/view:
@Html.TextBoxFor(x => x.Name, new { maxlength = "50", size = "50" })
The output generated is:
<input id="Name" maxlength="50" name="Name" size="50" type="text" value="" />
I am using the following doctype:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
My page is not validating using the HTML validation tool because of the closing /> of the input element. How do I get the helper to create me an input element looking like:
<input id="Name" maxlength="50" name="Name" size="50" type="text" value="">
I think that html generated by TextBoxFor
helper is correct or at least can be used in any browser, but if you want to create your own helper method, you can add a extension method to the HtmlHelper
. For example:
public static class MyExtension
{
public static MvcHtmlString MyTextBox(this HtmlHelper html, string var1, string var2)
{
StringBuilder tag= new StringBuilder();
// create your tag and wrtite it in string buffer
return MvcHtmlString.Create(tag.ToString());
}
}
Html generated by HTML helpers is XHTML valid and difference between HTML 4.01 strict and XHTML is that XHTML requires empty elements (as input, br, link, etc.) to close as in xml. I think that XHTML is more current standard and if there are no specific limitations you should use it, one of the reasons would be, that you HTML in such case can be used as XML (for XSLT processing and so on).
Html helpers in asp.net mvc are just the helpers that simplify html generation process. They do not include anything magical behind them, just render html. If you want to have absolute control over generated html, you can create your own helpers or write the pure html in your view. There's nothing wrong about that.
By the way, asp.net mvc 3 by default uses data-*
attributes that are part of html5. If you absolutely do not wish to use them, turn off in web.config
<appSettings>
<add key="ClientValidationEnabled" value="false" />
<add key="UnobtrusiveJavaScriptEnabled" value="false" />
</appSettings>
精彩评论