如何让文本框帮助程序显示 HTML 4.01 严格的有效 HTML 标记?

发布于 2024-12-06 15:07:15 字数 564 浏览 0 评论 0原文

我的页面/视图中有以下代码:

@Html.TextBoxFor(x => x.Name, new { maxlength = "50", size = "50" })

生成的输出是:

<input id="Name" maxlength="50" name="Name" size="50" type="text" value="" />

我正在使用以下文档类型:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

由于关闭/>,我的页面未使用 HTML 验证工具进行验证输入元素的。如何让助手为我创建一个如下所示的输入元素:

<input id="Name" maxlength="50" name="Name" size="50" type="text" value="">

I have the following code in my page/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="">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-12-13 15:07:15

我认为由 TextBoxFor helper 生成的 html 是正确的或者至少可以在任何浏览器中使用,但是如果您想创建自己的帮助器方法,您可以向 HtmlHelper 添加扩展方法。例如:

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());
  }
}

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());
  }
}
表情可笑 2024-12-13 15:07:15

HTML 帮助程序生成的 Html 是 XHTML 有效的,HTML 4.01 strict 和 XHTML 之间的区别在于 XHTML 需要空元素(如 input、br、link 等)才能像 xml 中那样关闭。
我认为 XHTML 是更当前的标准,如果没有特定限制,您应该使用它,原因之一是,在这种情况下,您的 HTML 可以用作 XML(用于 XSLT 处理等)。

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).

∞觅青森が 2024-12-13 15:07:15

asp.net mvc 中的 Html 助手只是简化 html 生成过程的助手。它们背后不包含任何神奇的东西,只是渲染 html。如果您想对生成的 html 有绝对的控制,您可以创建自己的帮助程序或在视图中编写纯 html。这并没有什么问题。

顺便说一下,asp.net mvc 3 默认使用 html5 的 data-* 属性。如果您绝对不想使用它们,请在 web.config 中关闭

  <appSettings>
    <add key="ClientValidationEnabled" value="false" />
    <add key="UnobtrusiveJavaScriptEnabled" value="false" />
  </appSettings>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文