【问题标题】:How do you add a boolean attribute in asp.net mvc?如何在 asp.net mvc 中添加布尔属性?
【发布时间】:2011-02-06 03:42:04
【问题描述】:

我正在尝试使用 asp.net mvc 2 中的 html 助手输出以下 html:

<input type="text" id="Name" name="Name" value="" autofocus />

我现在正在使用此代码:

<%= Html.TextBoxFor(x => x.Name) %>

我试过了

// results in <input ... autofocus="" />
<%= Html.TextBoxFor(x => x.Email, new { autofocus="" }) %>

// does not compile
<%= Html.TextBoxFor(x => x.Email, new { autofocus=null }) %>

有什么方法可以使用 html 帮助程序来完成我正在寻找的东西吗?

【问题讨论】:

    标签: asp.net-mvc-2 html-helper


    【解决方案1】:

    我认为您必须为此编写自己的助手。默认助手输出键/值对。在后台,它使用 TagBuilder 来构造标签。对于标签属性,它使用{0}="{1}" 格式来格式化属性及其值的键/值对。

    【讨论】:

    • 我给了它一些时间,看来你是对的,你是第一个,所以我会接受。我还是很难过。 ;)
    【解决方案2】:

    Html 助手生成 XHTML。您想要实现的是:

    1. 不是 XHTML(属性需要有值,甚至是空的)
    2. 不是 W3C 标准(输入标签没有 autofocus 属性)

    如果您想生成这样的标记,您需要手动完成或编写自己的帮助程序。

    【讨论】:

      【解决方案3】:

      我知道这是很久以前的事了,但是没有旧规则可以输入诸如

      之类的属性

      必填=“必填”

      这似乎适用于 FireFox、Chrome 和 Opera,但不适用于 Safari,当然也不适用于我公司的标准 IE8,(队列长号...Waa,waa,waa。;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-30
        • 2012-03-01
        • 2011-02-20
        • 1970-01-01
        • 2020-01-06
        • 2017-11-22
        • 1970-01-01
        • 2010-10-07
        相关资源
        最近更新 更多