简体   繁体   English

m的模态更新后的加载下拉列表

[英]Loading dropdown list after modal for m is updated

I very new to MVC and I have an issue that was real easy in webforms. 我对MVC非常陌生,但是在Web表单中却遇到了一个非常简单的问题。 I have a drop down list on a form. 我在表单上有一个下拉列表。 I have a button that loads a modal form that allows the user to add an item to the DB on the fly. 我有一个按钮,该按钮可加载模式窗体,该窗体允许用户将项目动态添加到数据库。 I then need to update the dropdown list with the new contents of the table. 然后,我需要使用表的新内容更新下拉列表。 I realize I need ajax to do this but I am very confused on how to initiate the update! 我知道我需要ajax来执行此操作,但是我对如何启动更新感到非常困惑!

I have the form with the following dropdown. 我有下面的下拉菜单。

            <div class="form-group form-group-sm required">
                @Html.LabelFor(m => m.ListingData.Transmission, new { @class = "col-md-4 control-label" })
                <div class="col-md-8">                        
                    @Html.DropDownListFor(m => m.ListingData.Transmission, (SelectList)ViewBag.TransmissionList, new { @class = "form-control"} )
                    @Html.ValidationMessageFor(m => m.ListingData.Transmission, "", new { @class = "text-danger" })
                    <p class="form-text"><a data-modal='' href='@Url.Action("QATransmission", "Admin", new { ListingId = Model.ListingData.ListingId}))' id=''>Quick Add Manufacturer</a></p>
                </div>
            </div>

I use this ajax in the main page to call the load the list code in the controller. 我在主页中使用此ajax调用加载控制器中的列表代码。

 $(function () {
        $.ajax({
            type: "GET",
            url: "Admin/LoadTransmissionList",
            data: JSON,
            cache: false,
            success: function (data) {
                $.each(data, function (index, value) {
                    $('#Transmission').append('<option value="' + value.TransmissionText + '">' + value.TransmissionText + '</option>');
                });
            }
        });
    });

This is my controller code for the list: 这是我的列表的控制器代码:

[HttpPost]
    public JsonResult LoadTransmissionList()
    {

        var lstTrans = db.Transmissions.ToList();
        List<SelectListItem> list = new List<SelectListItem>();


        foreach (Transmission p in lstTrans)
        {
            list.Add(new SelectListItem() { Value = p.TransmissionText.ToString(), Text = p.TransmissionText.ToString() });
        }


        return Json(list, JsonRequestBehavior.AllowGet);
    }

I have a modal form and it has a text box for entering a new list item. 我有一个模式窗体,它有一个用于输入新列表项的文本框。 I need to save to the DB, which works but then! 我需要保存到数据库,但是可以! I cant come up with the reload the listbox contents part. 我无法提出重新加载列表框内容部分。 Any help! 任何帮助!

Thanks! 谢谢!

1)Make your code changes like this 1)像这样进行代码更改

    success: function (data) {
 $('#ListingData_Transmission').html("")
                    $.each(data, function (index, value) {
                        $('#ListingData_Transmission').append('<option value="' + value.TransmissionText + '">' + value.TransmissionText + '</option>');
                    });
                }

hope this should work now 希望这应该现在工作

2) Make your form as a partial view and reload it whenever your saving something to DB. 2)将表单作为部分视图,每当将内容保存到DB时都重新加载它。

Let me know if any issues 让我知道是否有任何问题

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

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