Given the following classes and controller action method:
public School
{
public Int32 ID { get; set; }
publig String Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street1 { get; set; }
public string City { get; set; }
public String ZipCode { get; set; }
public String State { get; set; }
public String Country { get; set; }
}
[Authorize(Roles = "SchoolEditor")]
[AcceptVerbs(HttpVerbs.Post)]
public SchoolResponse Edit(Int32 id, FormCollection form)
{
School school = GetSchoolFromRepository(id);
UpdateModel(school, form);
return new SchoolResponse() { School = school };
}
And the following form:
<form method="post">
School: <%= Html.TextBox("Name") %><br />
Street: <%= Html.TextBox("Address.Street") %><br />
City: <%= Html.TextBox("Address.City") %><br />
Zip Code: <%= Html.TextBox("Address.ZipCode") %><br />
Sate: <select id="Address.State"></select><br />
Country: <select id="Address.Country"></select><br />
</form>
I am able to update both the School instance and the Address member of the school. This is quite nice! Thank you ASP.NET MVC team!
However, how do I use jQuery to select the drop down list so that I can pre-fill it? I realize that I could do this server side but there will be other dynamic elements on the page that affect the list.
The following is what I have so far, and it does not work as the selectors don't seem to match the IDs:
$(function() {
$.getJSON("/Location/GetCountryList", null, function(data) {
$("#Address.Country").fillSelect(data);
});
$("#Address.Country").change(function() {
$.getJSON("/Location/GetRegionsForCountry", { country: $(this).val() }, function(data) {
$("#Address.State").fillSelect(data);
});
});
});
The Release Candidate of ASP.NET MVC that was just released fixed this issue, it now replaces the dots with underscores for the ID attribute.
Renders to
For more information view the release notes, starting on page 14.
This is documented in jQuery Selectors docs:
In short, prefix the
.
with\\
as follows:Why doesn't
.
work in my ID?The problem is that
.
has special significance, the string following is interpreted as a class selector. So$('#Address.Country')
would match<div id="Address" class="Country">
.When escaped as
\\.
, the dot will be treated as normal text with no special significance, matching the ID you desire<div id="Address.Country">
.This applies to all the characters
!"#$%&'()*+,./:;<=>?@[\]^`{|}~
which would otherwise have special significance as a selector in jQuery. Just prepend\\
to treat them as normal text.Why 2
\\
?As noted in bdukes answer, there is a reason we need 2
\
characters.\
will escape the following character in JavaScript. Se when JavaScript interprets the string"#Address\.Country"
, it will see the\
, interpret it to mean take the following character litterally and remove it when the string is passed in as the argument to$()
. That means jQuery will still see the string as"#Address.Country"
.That's where the second
\
comes in to play. The first one tells JavaScript to interpret the second as a literal (non-special) character. This means the second will be seen by jQuery and understand that the following.
character is a literal character.Phew! Maybe we can visualize that.
From Google Groups:
So, I guess you're looking at
Also check out How do I select an element by an ID that has characters used in CSS notation? on the jQuery FAQ.
You can't use a jQuery id selector if the id contains spaces. Use an attribute selector:
If possible, specify element type as well:
Escape it for Jquery:
usage example:
more info here.
Using two Backslashes it´s ok, it´s work. But if you are using a dynamic name, I mean, a variable name, you will need to replace characters.
If you don´t wan´t to change your variables names you can do this:
and than you have your jquery object.