给定以下类和控制器动作方法:

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>

我可以更新学校实例和学校成员的地址.这真是太棒了!感谢ASP.NET MVC团队!

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.

以下是我到目前为止拥有的内容,它不起作用,因为 Select 器似乎与ID不匹配:

$(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);
    });
  });
});

推荐答案

Google Groups:

在每个特殊字符前使用两个反斜杠.

jQuery Select 器中的反斜杠转义下一个字符.但你需要

So, I guess you're looking at

$(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);
    });
  });
});

Also check out How do I select an element by an ID that has characters used in CSS notation? on the jQuery FAQ.

Jquery相关问答推荐

使用动态类名的gm_addStyle?

jQuery从JSON创建嵌套列表

在 Laravel 中使用 jQuery post 按相关值过滤 Select 选项,如何显示从控制器返回的数据?

为什么 .join() 不能与函数参数一起使用?

使用 jQuery 时何时/为什么要在变量前加上$?

在 Bootstrap 3 中向工具提示添加换行符

Twitter Bootstrap 是否包含 jQuery?

使用 jQuery Validate 确认密码

如何禁用在div内单击

在没有 jQuery 的情况下在 node.js 上合并或合并 JSON

子元素点击事件触发父点击事件

使用 jQuery 从 JavaScript 对象中添加/删除项目

C# String.IsNullOrEmpty Javascript 等效项

如何在 Bootstrap 中创建切换按钮

5秒后jQuery自动隐藏元素

Select jQuery UI 自动完成后清除表单字段

使用 Jquery 查找父 div 的 id

textarea 的 val() 与 text()

jquery ui Dialog:无法在初始化之前调用对话框上的方法

jQuery:通过 .attr() 添加两个属性;方法