【问题标题】:populate dropdown options based on another dropdown selection in mvc根据 mvc 中的另一个下拉选择填充下拉选项
【发布时间】:2017-07-04 11:46:34
【问题描述】:

我有 2 个下拉列表(即)mutualExcParam。它有 3 个选项。根据选择的此选项,需要填充另一个下拉列表(即)agencyRetrieve。 这是通过 JSON 请求完成的,并且可以很好地处理较少的记录。如果记录更多计数> 500,则需要加载时间。当我循环通过它时,有没有其他方法可以做同样的事情。

$(document).ready(function () { 
$('.mutualExcParam').change(function () { // calling by class name on 1st dropdown
    var agencyRetrieveId = $(this).attr('id') + '_'; // 2nd dropdown id

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        url: "../Report/FillAgencyValue",
        data: JSON.stringify({ agencyId: agencyId}),
        success: function (data) {
            $('#' + agencyRetrieveId).html('');
            var optionhtml1 = '<option value="All">Select All</option>';
            $('#' + agencyRetrieveId).append(optionhtml1);
            $(data).each(function () {
             $('#' + agencyRetrieveId).append($("<option></option>").val(this.Value).html(this.Text));    
            });
            $(".selectpicker").selectpicker('refresh');
        }
    });
});
});

 [HttpPost]
    public ActionResult FillAgencyValue(int agencyId, string fromDate, string toDate, int salesArea, string[] salesGroup)
    {
        AppCode.CommonCommands commonCommands = new AppCode.CommonCommands();

        PopulateControlData populateControlData = new PopulateControlData();
        // Get all Sales Area
        DataTable dtsalesArea = commonCommands.GetAgencyDropdownData(agencyId, fromDate, toDate, salesArea, salesGroup);

        populateControlData.agencyValues = new List<SelectListItem>();
        if (dtsalesArea.Rows.Count > 0)
        {

            populateControlData.agencyValues = (from DataRow row in dtsalesArea.Rows
                                                select new SelectListItem
                                                {
                                                    Text = row["ParameterLabel"].ToString(),
                                                    Value = row["ParameterValue"].ToString()
                                                }).ToList();
        }
        return Json(populateControlData.agencyValues, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

  • 您可以考虑从您的FillAgencyValue 返回 HTML。
  • 是的。还添加了控制器代码和平。

标签: jquery json asp.net-mvc asp.net-mvc-4 asp.net-mvc-5


【解决方案1】:

我已修改您的代码以返回 HTML。我没有包含select all 选项,这可以通过在控制器方法中的返回之前执行插入来添加。

我没有测试过代码,只是概念。

$(document).ready(function () { 
$('.mutualExcParam').change(function () { // calling by class name on 1st dropdown
    var agencyRetrieveId = $(this).attr('id') + '_'; // 2nd dropdown id

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        url: "../Report/FillAgencyValue",
        data: { agencyId: agencyId},
        success: function (data) {
            $('#' + agencyRetrieveId).empty();
            $('#' + agencyRetrieveId).html(data);
        }
    });
});
});

[HttpPost]
public ActionResult FillAgencyValue(int agencyId, string fromDate, string toDate, int salesArea, string[] salesGroup)
{
    AppCode.CommonCommands commonCommands = new AppCode.CommonCommands();

    PopulateControlData populateControlData = new PopulateControlData();
    // Get all Sales Area
    DataTable dtsalesArea = commonCommands.GetAgencyDropdownData(agencyId, fromDate, toDate, salesArea, salesGroup);

    List<string> lst = new List<string>();
    if (dtsalesArea.Rows.Count > 0)
    {

        lst = (from DataRow row in dtsalesArea.Rows
               select new string.Format("<option value='{0}'>{1}</option>", row["ParameterValue"], row["ParameterLabel"])).ToList();
    }
    return Content(string.Join(lst, "\n"));
}

【讨论】:

  • 谢谢你的想法。我也试过了。如果我添加这个数据:JSON.stringify({ AgencyId: AgencyId}) 那么它只会命中控制器方法 FillAgencyValue.. 返回时面临问题来自控制器的列表
  • 很可能在获取此方法时出错,因为在您的 AJAX 调用中存在预期但未定义的参数。例如:salesArea 在您的浏览器中调试 (F12) 应该会显示这一点。
  • 当我传递这样的数据时: JSON.stringify({ AgencyId: AgencyId, fromDate: $('#From-10').val(), toDate: $('#To-11' ).val(), salesArea: $('#Area_1').val() }),我能够获取数据.. 非常感谢它对我有用..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多