C# MVC下拉列表,onchange
我在html中有两个Dropdownlistfor,Dropdownlistfor Country和Dropdownlistfor state。我使用MVC,我想像这样输出,如果我改变所选国家,所选国家的状态将填入dropdownlistfor状态。我从数据库中获取国家和州的数据,州表中有一个countryId字段。谢谢你的帮助 下面是Html中的示例代码C# MVC下拉列表,onchange,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我在html中有两个Dropdownlistfor,Dropdownlistfor Country和Dropdownlistfor state。我使用MVC,我想像这样输出,如果我改变所选国家,所选国家的状态将填入dropdownlistfor状态。我从数据库中获取国家和州的数据,州表中有一个countryId字段。谢谢你的帮助 下面是Html中的示例代码 <div class="fieldBlock"> <div>
<div class="fieldBlock">
<div><label>Country</label> *</div>
<div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div>
@Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation)
<div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>
<div class="fieldBlock">
<div>@Html.LabelFor(model => model.Employee.StateId, "State/Province") *</div>
<div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305" })</div>
<div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>
国家*
@Html.DropDownListFor(model=>model.Country.Id,model.CountrySelectList,新的{@class=“width305”,@Id=“ddlCountry”})
@Html.HiddenFor(model=>model.Country.Name)@Html.HiddenFor(model=>model.Country.缩写)
@Html.ValidationMessageFor(model=>model.Employee.StateId)
@Html.LabelFor(model=>model.Employee.StateId,“州/省”)*
@Html.DropDownListFor(model=>model.Employee.StateId,model.CountryStateProvinceSelectList,新的{@class=“width305”})
@Html.ValidationMessageFor(model=>model.Employee.StateId)
试试这个
鉴于
<script type="text/javascript">
$(function() {
$("#ddlCountry").change(function() {
var selectedItem = $(this).val();
var ddlStates = $("#ddlState");
var statesProgress = $("#states-loading-progress");
statesProgress.show();
$.ajax({
cache: false,
type: "GET",
url: "@(Url.Action("SomeAction", "SomeController"))",
data: { "countryId": selectedItem},
success: function (data) {
ddlStates.html('');
$.each(data, function(id, option) {
ddlStates.append($('<option></option>').val(option.id).html(option.name));
});
},
error:function (xhr, ajaxOptions, thrownError){
alert('Failed to retrieve states.');
}
});
});
});
</script>
<div class="fieldBlock">
<div><label>Country</label> *</div>
<div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div>
@Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation)
<div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>
<div class="fieldBlock">
<div>@Html.LabelFor(model => model.Employee.StateId, "State/Province") *</div>
<div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305", @id = "ddlState" })</div>
<div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>
注意:检查
Id
是否正确绑定到下拉列表,并且我已经编写了用于获取状态的伪代码。可能的重复,如果您知道,你实际上问了一个问题,而不是简单地陈述一大堆事情,期望我们猜测你想要什么。我认为你需要在答案的控制器部分进行编辑。操作的返回类型必须是JsonResult
。
public ActionResult SomeAction(string countryId)
{
//perform your action here
var states = _stateProvinceService.GetStateProvincesByCountryId(country.Id).ToList();
var result = (from s in states
select new { id = s.Id, name = s.GetLocalized(x => x.Name) }
).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}