简体   繁体   English

MVC 4 - 级联下拉列表 - Ajax JavaScript调用问题

[英]MVC 4 - Cascading Dropdown Lists - Issue with Ajax JavaScript Call

I have an MVC 4 app with a View containing two dropdown lists. 我有一个MVC 4应用程序,其中包含两个下拉列表。 The user selects a value in the first dropdown and then an Ajax call is made to populate the second dropdown based on the contents of the first. 用户在第一个下拉列表中选择一个值,然后进行Ajax调用以根据第一个下拉列表的内容填充第二个下拉列表。

My JavaScript code looks as follows and gets called when the user selects an item in the first dropdown: 我的JavaScript代码如下所示,当用户在第一个下拉列表中选择一个项时,它会被调用:

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++) {
                **if (data[x].Selected) {**
                    markup += "<option selected='selected' value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                else
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $('#autoModel').html(markup).show();
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });
}

The Ajax call works correctly. Ajax调用正常工作。 However, the data that gets returned for the second dropdown contains a selected item and I'm trying to detect the selected item (via the 'if' statement), and render the HTML appropriately. 但是,为第二个下拉列表返回的数据包含一个选定的项目,我正在尝试检测所选项目(通过'if'语句),并适当地呈现HTML。 The problem is that 'Selected' doesn't seem to be a property of 'data' because each value evaluates to false, even though one of the values is true. 问题是'Selected'似乎不是'data'的属性,因为每个值的计算结果为false,即使其中一个值为true。

Am I doing something wrong? 难道我做错了什么? Or is there a better way to do this? 或者有更好的方法吗?

The following is the controller code: 以下是控制器代码:

[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 object
    SelectList returnList = new SelectList(modelList, "AutoModelId", "Description");

    // Now find the selected model in the list and set it to selected.
    foreach (var item in returnList)
    {
        if (item.Value == modelId.ToString())
            item.Selected = true;
    }

    return Json(returnList);
}

Try this instead (add modelId to constructor of SelectList, and remove the foreach block): 试试这个(将modelId添加到SelectList的构造函数中,并删除foreach块):

// Convert all the model data to a SelectList object
SelectList returnList = new SelectList(modelList, "AutoModelId", "Description", modelId);

return Json(returnList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM