我在视图中填充了两个 DropDownList:
<tr>
<td class="info_label">City</td>
<td>@Html.DropDownListFor(m=>m.User.City,Model.City,"-- Chọn thành phố --",new { @class = "dropdown" })</td>
</tr>
<tr>
<td class="info_label">Ward</td>
<td>@Html.DropDownListFor(m => m.User.Ward, Model.Ward, "-- Chọn quận --" ,new { @class = "dropdown" })</td>
</tr>
当我改变第一个 ddl 的值时,第二个 ddl 也会改变取决于第一个 ddl 的值。我有一个 jquery 脚本来实现这一点。这是我的jQuery代码:
function LocationValue() {
$("#User_City").change(function () {
var cityName = $("#User_City").val();
if (cityName == null) {
$("Ward").prop("disabled", true);
}
else {
$("Ward").prop("disabled", false);
getWards(cityName);
}
});
}
function getWards(cityName) {
$.ajax({
url: "/Checkout/Wards",
data: { CityName: cityName },
dataType: "json",
type: "POST",
error: function () {
return;
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#User_Ward").html(items);
}
});
}
和我的控制器中的 Post 方法:
[HttpPost]
public ActionResult Cities()
{
var cities = ILocaRepo.Cities;
return Json(new SelectList(cities,"CityID","CityName"));
}
[HttpPost]
public ActionResult Wards(string CityName)
{
var wards = ILocaRepo.ListWardByCityName(CityName);
return Json(new SelectList(wards, "WardID", "WardName"));
}
出于某种原因,2 ddl 的来源有Value = x.CityName.ToString()
,没有 CityID(int)。在 firebud 中,为什么第二个 ddl 的响应总是[]
. 对我的错误有什么建议吗?