单击按钮时,未选择“案例收集”下拉列表

时间:2019-06-16 14:24:00

标签: asp.net-mvc asp.net-ajax

我有两个下拉列表。第一个下拉列表由ViewModel填充。使用Ajax(级联下拉列表)填充辅助下拉列表。我做得很好 但是在单击按钮并返回到同一页面后,第二个下拉列表未被选中或返回默认值。下面是我的代码,谢谢

 @Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.country, new { @class = "form-control" })
 @Html.DropDownListFor(model => model.AddNewThirdParty, (List<SelectListItem>)ViewBag.thirdPartyDDL, new { @class = "form-control" })
 <input type="button" value="Add New" name="action:AddNewThirdParty" style="@displayButtonAddThirdParty" onclick="return addnewthirdparty();" />

 $(document).ready(function () {
     $("#PaymentCode").change(function () {
        $("#AddNewThirdParty").empty();
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetInsuranceCompany")',
            dataType: 'json',
            data: { code: $("#PaymentCode").val() },
            success: function (states) {

                $.each(states, function (i, state) {

                    $("#AddNewThirdParty").append('<option value="' + state.Value + '">' +
                        state.Text + '</option>');
                });
            },
            error: function (ex) {
                alert('Failed to retrieve states.' + ex);
            }
        });
        return false;
     })
 });

控制器

public ActionResult GetInsuranceCompany(string code, string filter)
{
    string codeJP = string.Empty;
    ParamJenisPembayaran paramJP = db.ParamJenisPembayarans.Where(x => x.Code == code).FirstOrDefault();
    if (paramJP != null) codeJP = paramJP.Name;
    var states1 = db.InsuranceCompany_CSFs.Where(x => x.JenisPembayaran == codeJP).ToList();
    List<SelectListItem> listates = new List<SelectListItem>();
    listates.Add(new SelectListItem { Text = "select_thrid_party", Value = " " });
    if (states1 != null){
        foreach (var x in states1){
           listates.Add(new SelectListItem { Text = x.CompanyID + " - " + x.CompanyName, Value = x.CompanyID });
        }
    }
    return Json(new SelectList(listates, "Value", "Text", JsonRequestBehavior.AllowGet));
}

 public ActionResult AddNewContractNo(ParamBlacklistPembayaranViewModel model){
   string[] arrContractNumber = string.IsNullOrEmpty(model.ContractNumber) ? null : model.ContractNumber.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
   ViewBag.ContractNumberList = arrContractNumber;
   return View("Create", model);
 }

1 个答案:

答案 0 :(得分:1)

使用change()事件时,将仅在第一个下拉菜单中发生更改的地方填充第二个下拉列表,因此您可以更改代码,以便在页面加载时也可以填充第二个下拉列表。 尝试以下代码:

$(document).ready(function () {
             populateCascadeList();
             $("#PaymentCode").change(function () {
                 populateCascadeList();
                return false;
             })
         });
}

 function populateCascadeList(){
        $("#AddNewThirdParty").empty();
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("GetInsuranceCompany")',
                    dataType: 'json',
                    data: { code: $("#PaymentCode").val() },
                    success: function (states) {

                        $.each(states, function (i, state) {

                            $("#AddNewThirdParty").append('<option value="' + state.Value + '">' +
                                state.Text + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve states.' + ex);
                    }
                });
}