转自:
二级联动:
View:
<script type=
"text/javascript"
>
$(function () {
$(
"#drpProvince"
).change(function () {
$(
"#drpCity"
).
get
(0).options.length = 0;
//清空
$.getJSON(
"/Persons/GetCities/"
+ $(
this
).val(),
null
, function (data) {
$.each(data, function (i, item) {
$(
"<option></option>"
).val(item[
"ID"
]).text(item[
"CityName"
]).appendTo($(
"#drpCity"
));
});
});
});
});
</script>
@Html.DropDownList(
"drpProvince"
,
null
,
"请选择"
)
@Html.DropDownList(
"drpCity"
,
null
,
"请选择"
)
Action:
public
ActionResult Create()
{
List<Provinces> list = db.Provices.ToList();
ViewData[
"drpProvince"
] =
new
SelectList(list,
"ID"
,
"ProvinceName"
);
ViewData[
"drpCity"
] =
new
List<SelectListItem>();
return
View();
}
//返回城市列表
public
ActionResult GetCities(
int
? id)
{
List<Cities> list = db.Cities.Where(q => q.ProvincesID == id).ToList();
if
(Request.IsAjaxRequest())
{
return
Json(list, JsonRequestBehavior.AllowGet);
}
else
{
return
View(
""
);
}
}