Javascript 使用minimalect更新DropDownList

Javascript 使用minimalect更新DropDownList,javascript,jquery,asp.net,ajax,asp.net-mvc,Javascript,Jquery,Asp.net,Ajax,Asp.net Mvc,好的,现在的场景是,我正在用下面的代码填充模型中的下拉列表 ViewBag.LeaseCompanyID = new SelectList(ContractModelEntity.system_supplier.Where(x => x.Type == "Lease"), "CompanyID", "Name", data.LeaseCompanyID); 这非常有效,但是在我的表单上,下拉列表旁边有一个按钮,它使用ajax和一个模式弹出窗口在数据库中添加了另一个选项 这里是这个的控制器

好的,现在的场景是,我正在用下面的代码填充模型中的下拉列表

ViewBag.LeaseCompanyID = new SelectList(ContractModelEntity.system_supplier.Where(x => x.Type == "Lease"), "CompanyID", "Name", data.LeaseCompanyID);
这非常有效,但是在我的表单上,下拉列表旁边有一个按钮,它使用ajax和一个模式弹出窗口在数据库中添加了另一个选项

这里是这个的控制器代码

[HttpPost]
    public JsonResult AddSupplier([Bind(Include="Name,Type")] system_supplier data)
    {
        if (ModelState.IsValid)
        {
            ContractModelEntity.system_supplier.Add(data);
            ContractModelEntity.SaveChanges();
            return Json(0, JsonRequestBehavior.AllowGet);
        }
        return Json(1, JsonRequestBehavior.AllowGet);
    }
当新选项添加到数据库中时,我需要刷新我的dropdownlist以获取当前的新数据。如果我刷新页面,我可以看到新选项。我正在使用minimalect插件进行下拉列表

如果有人知道更新这个minimalect列表的方法,那么肯定有一种方法可以通过ajax调用来构建列表,该调用返回一些JSON数据


提前感谢您的帮助

好的,在做了一些研究之后,这里是我的解决方案,希望它能帮助其他人。有些人甚至可能有更干净的解决方案

我首先创建了一个jsonresult控制器方法,如下所示

[HttpGet]
    public JsonResult RetreiveSuppliers(string contractType)
    {
        var supplierData = ContractModelEntity.system_supplier.Where(x => x.Type == contractType);
        var result = new List<object>();
        foreach (var x in supplierData)
        {
            result.Add(new { Id = x.CompanyID, Name = x.Name });
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }
$("body").on("click", "#btn_InsertNewSupplier", function () {
        var supForm = $("#addSupData");
        $.ajax({
            url: "@Url.Action("AddLeaseSupplier", "Contract")",
            data: supForm.serialize(),
            type: "POST",
            success: function (result) {
                if (result === 0) {
                    var inst = $.remodal.lookup[$('[data-remodal-id=modal_AddSupplier]').data('remodal')];
                    inst.close();
                    NotificationAlert("success", "New Supplier Created");
                    GetNewSupplierList();
                } else {
                    NotificationAlert("error", "Failed Adding New Supplier");
                }
            }
        });
    });

    function GetNewSupplierList() {
        var actionurl = "@Url.Action("RetreiveSuppliers", "Contract", new { contractType = "Lease"})";
        $.getJSON(actionurl, tempdata);
    }

    function tempdata(response) {
        if (response != null) {
            var html = "";
            for (var i = 0; i < response.length; i++) {
                html += '<option value="' + response[i].Id + '">' + response[i].Name + '</option>';
            }
            $("#LeaseCompanyID").html(html);
        }
    }
这让我得到了数据库中的数据。然后我在页面上创建了一个javascript,如下所示

[HttpGet]
    public JsonResult RetreiveSuppliers(string contractType)
    {
        var supplierData = ContractModelEntity.system_supplier.Where(x => x.Type == contractType);
        var result = new List<object>();
        foreach (var x in supplierData)
        {
            result.Add(new { Id = x.CompanyID, Name = x.Name });
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }
$("body").on("click", "#btn_InsertNewSupplier", function () {
        var supForm = $("#addSupData");
        $.ajax({
            url: "@Url.Action("AddLeaseSupplier", "Contract")",
            data: supForm.serialize(),
            type: "POST",
            success: function (result) {
                if (result === 0) {
                    var inst = $.remodal.lookup[$('[data-remodal-id=modal_AddSupplier]').data('remodal')];
                    inst.close();
                    NotificationAlert("success", "New Supplier Created");
                    GetNewSupplierList();
                } else {
                    NotificationAlert("error", "Failed Adding New Supplier");
                }
            }
        });
    });

    function GetNewSupplierList() {
        var actionurl = "@Url.Action("RetreiveSuppliers", "Contract", new { contractType = "Lease"})";
        $.getJSON(actionurl, tempdata);
    }

    function tempdata(response) {
        if (response != null) {
            var html = "";
            for (var i = 0; i < response.length; i++) {
                html += '<option value="' + response[i].Id + '">' + response[i].Name + '</option>';
            }
            $("#LeaseCompanyID").html(html);
        }
    }
因此,一旦ajax调用成功,它将触发GetNewSupplierList函数,该函数调用我的控制器方法并返回一些JSON数据。一旦返回,它将调用tempdata,它将为我的select picker构建新的HTML,一旦构建完成,它将更新selectpicker id上的HTML

工作就像一个魅力

感谢所有看过这篇文章的人