Skip to content Skip to sidebar Skip to footer

Preventing Asp.net Mvc From Replacing Period With Underscore In Html Helper Ids

Having just upgraded to the latest ASP.NET MVC Release Candidate I noticed that, when using Html Helpers, any name with a period '.' in it will have this replaced by an underscore

Solution 1:

From the ASP.NET MVC RC1 Release notes (page 15).

In this release, by default the dot character is automatically replaced with an underscore in the value of the ID attribute. Thus the example TextBox renders the following markup:

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

To change the default replacement character, you can set the HtmlHelper.IDDotReplacementChar property to the character that you want to use instead.

FYI. Looking at the source code at http://www.codeplex.com/aspnet, it appears that the real name of the property in RC1 is IdAttributeDotReplacement. The relevant code snippet is below. To keep the dot, you'd just set this property to the dot character -- i.e., replace the dot character with itself.

publicstaticstring IdAttributeDotReplacement {
    get {
        if (String.IsNullOrEmpty(_idAttributeDotReplacement)) {
            _idAttributeDotReplacement = "_";
        }
        return _idAttributeDotReplacement;
    }
    set {
        _idAttributeDotReplacement = value;
    }
}

Post a Comment for "Preventing Asp.net Mvc From Replacing Period With Underscore In Html Helper Ids"