[英]MVC - Cascading Dropdown Lists - How to get the selected value?
我有一個帶有級聯下拉列表的View的MVC應用程序。 在用戶在第一個下拉列表中選擇了值之后,我有以下Ajax調用來檢索第二個下拉列表的數據:
function GetAutoModel(_manufacturerId) {
var autoSellerListingId = document.getElementById("AutoSellerListingId").value;
$.ajax({
url: "/AutoSellerListing/GetAutoModel/",
data: { manufacturerId: _manufacturerId, autoSellerListingId: autoSellerListingId },
cache: false,
type: "POST",
success: function (data) {
var markup = "<option value='0'>-- Select --</option>";
for (var x = 0; x < data.length; x++) {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
$('#ModelList').html(markup).show();
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
調用以下控制器代碼以提供Ajax調用數據:
[HttpPost]
public ActionResult GetAutoModel(int manufacturerId, int autoSellerListingId)
{
int modelId = 0;
// Get all the models associated with the target manufacturer
List<AutoModel> modelList = this._autoLogic.GetModelListByManufacturer(manufacturerId);
// If this is an existing listing, get the auto model Id value the seller selected.
if (autoSellerListingId > 0)
modelId = this._systemLogic.GetItem<AutoSellerListing>(row => row.AutoSellerListingId == autoSellerListingId).AutoModel.AutoModelId;
// Convert all the model data to a SelectList and return it
SelectList returnList = new SelectList(modelList, "AutoModelId", "Description", modelId);
return Json(returnList);
}
注意控制器代碼(modelId)中new SelectList()
調用中的最后一個參數。 這就是我希望在Ajax調用中創建select對象后將所選值設置為。 問題是我不知道如何在客戶端中訪問此值。 一切正常,我只是不知道如何訪問所選值,然后設置它。
問題是我不知道如何在客戶端中訪問此值。
您可以查看客戶端上的Selected
boolean屬性:
if (data[x].Selected) {
markup += "<option value=" + data[x].Value + " selected=\"selected\">" + data[x].Text + "</option>";
} else {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.