阻止ASP.NET MVC在Html Helper ID中使用下划线替换句点

Pau*_*non 7 javascript asp.net-mvc jquery prototype

刚刚升级到最新的ASP.NET MVC候选版本时,我注意到,当使用Html Helpers时,任何带有句点"."的名称.当输出元素的ID时,它将被下划线"_"替换.

我相信这是为了帮助使用JQuery,并且使用句点是为了帮助使用ModelBinders.这打破了我们使用原型的所有javascript,因为ID已全部更改.

有没有办法轻松关闭此功能?

tva*_*son 14

从ASP.NET MVC RC1发行说明(第15页).

在此版本中,默认情况下,点字符会自动替换为ID属性值中的下划线.因此,示例TextBox呈现以下标记:

<input type="text" name="Person.FirstName" id="Person_FirstName" />

若要更改默认替换字符,可以将HtmlHelper.IDDotReplacementChar属性设置为要使用的字符.

仅供参考.查看http://www.codeplex.com/aspnet上的源代码,看来RC1中属性的真实名称是IdAttributeDotReplacement.相关的代码段如下.要保留点,您只需将此属性设置为点字符 - 即,将点字符替换为自身.

public static string IdAttributeDotReplacement {
    get {
        if (String.IsNullOrEmpty(_idAttributeDotReplacement)) {
            _idAttributeDotReplacement = "_";
        }
        return _idAttributeDotReplacement;
    }
    set {
        _idAttributeDotReplacement = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 似乎是一种奇怪的方式 - 假设它是帮助JQuery默认的.我添加了HtmlHelper.IdAttributeDotReplacement ="."; 到Global Application_Start,我们在8分钟后重新上线;)谢谢 (2认同)