How do I get jQuery to select elements with a . (period) in their ID?

后端 未结 8 1257
南笙
南笙 2020-11-22 13:16

Given the following classes and controller action method:

public School
{
  public Int32 ID { get; set; }
  publig String Name { get; set; }
  public Address         


        
8条回答
  •  借酒劲吻你
    2020-11-22 13:39

    Just additional information: Check this ASP.NET MVC issue #2403.

    Until the issue is fixed, I use my own extension methods like Html.TextBoxFixed, etc. that simply replaces dots with underscores in the id attribute (not in the name attribute), so that you use jquery like $("#Address_Street") but on the server, it's like Address.Street.

    Sample code follows:

    public static string TextBoxFixed(this HtmlHelper html, string name, string value)
    {
        return html.TextBox(name, value, GetIdAttributeObject(name));
    }
    
    public static string TextBoxFixed(this HtmlHelper html, string name, string value, object htmlAttributes)
    {
        return html.TextBox(name, value, GetIdAttributeObject(name, htmlAttributes));
    }
    
    private static IDictionary GetIdAttributeObject(string name)
    {
        Dictionary list = new Dictionary(1);
        list["id"] = name.Replace('.', '_');
        return list;
    }
    
    private static IDictionary GetIdAttributeObject(string name, object baseObject)
    {
        Dictionary list = new Dictionary();
        list.LoadFrom(baseObject);
        list["id"] = name.Replace('.', '_');
        return list;
    }
    

提交回复
热议问题