Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

javascript - 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 "_" when the element's ID is output.

I believe this is to aid in using JQuery and the use of the period is to aid in the use of ModelBinders. This has broken all of our javascript, which uses prototype, as the IDs have all changed.

Is there a way to turn this feature off easily?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...