我在查看填充2周的DropDownList:第二個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也改變取決於第一的價值。我有一個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的repsone始終是[]
。任何暗示我的錯誤?
有點困惑,因爲你有一個名爲「城市」的下拉列表中,但詮釋,他的JS標記,你指的是一個「User_City」。 –
因爲在firebug中,我檢查ddl並查看它的ID是'User_City',名字是'User.City'。 – Luffy