给定以下类和控制器操作方法:
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 };
}
和以下形式:
<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团队!
但是,如何使用jQuery选择下拉列表,以便可以预先填充它?我意识到我可以在服务器端这样做,但是页面上会有其他影响列表的动态元素。
以下是我到目前为止所拥有的,它不工作,因为选择器似乎与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);
});
});
});
发布于 2009-03-20 18:35:43
如果id包含空格,则不能使用jQuery id选择器。使用属性选择器:
$('[id=foo bar]').show();
如果可能,还应指定元素类型:
$('div[id=foo bar]').show();
发布于 2014-04-28 19:34:20
使用Jquery将其转义:
function escapeSelector(s){
return s.replace( /(:|\.|\[|\])/g, "\\$1" );
}
使用示例:
e.find('option[value='+escapeSelector(val)+']')
更多信息here。
发布于 2009-01-29 20:57:32
刚刚发布的ASP.NET MVC候选版本修复了这个问题,它现在用ID属性的下划线替换了圆点。
<%= Html.TextBox("Person.FirstName") %>
渲染到
<input type="text" name="Person.FirstName" id="Person_FirstName" />
有关更多信息,请查看从第14页开始的发行说明。
https://stackoverflow.com/questions/350292
复制相似问题