简体   繁体   English

如何在其他下拉列表中选择ASP .NET MVC填充下拉列表

[英]How to populate drop down list on other drop down list selected asp .net mvc

(sorry but I've searched related questions but ive only found this without answer: Drop-down list not populating other controls ) (对不起,但是我已经搜索了相关问题,但是我只找到了这个,却没有答案: 下拉列表未填充其他控件

okay so I'am stuck here, first of all, I'am strickly backend .net developer but Im in situation where I need to help, okay so the problem is: i dont know how to populate second combo box which depends on the first one, for example, there are clients and contacts, every client has own contacts, so depending on which client you select, we need to show his contacts: how to do this? 好的,所以我被困在这里,首先,我是后端.net开发人员,但是在需要帮助的情况下,我的问题是:我不知道如何填充依赖于第一个的第二个组合框例如,有一个客户和联系人,每个客户都有自己的联系人,因此根据您选择的客户,我们需要显示他的联系人:该怎么做? on the wpf platform, we simply have a on item selection changed event, something like that, and on changed we update the second combo box, but i have no idea how to do this in asp.net mvc, i pouplate the first combo box like this: 在wpf平台上,我们只是有一个on item selection更改事件,类似的事情,并且在更改时我们更新了第二个组合框,但是我不知道如何在asp.net mvc中执行此操作,我将第一个组合框像这样:

<div class="form-group">
            @Html.LabelFor(model => model.ClientID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ClientID", TempClass.GetClients, "Select Client")
            </div>
        </div>

Where TempClass.GetClients returns TempClass.GetClients返回的位置

IEnumerable<SelectListItem>

with values of: ID and the name of the client. 值:ID和客户端名称。

Hope you understand, Thanks in advance. 希望您能理解,谢谢。

Basically, you need to do the following step: 基本上,您需要执行以下步骤:

Using Jquery to catch the event of change in dropdown list http://api.jquery.com/change/ 使用Jquery捕获下拉列表http://api.jquery.com/change/中的更改事件

$("#Dropdown1").on("change", function(){
    var selectedValue = $(this).val();
    AjaxCall(selected);
})

Then, use Ajax to make a call to an action on controller which populate values for the second dropdown. 然后,使用Ajax调用控制器上的操作,该操作将填充第二个下拉列表的值。

function AjaxCall(value){
    $.ajax({
        type: "GET",
        url : 'UrlToAction',
        data: { value: value },
        success: function(data) {
            PopulateValueForSecondDropdown(data);
        }
    });
}

Once you receive data from Ajax, in the success method, use jquery to populate values for the second dropdown 从Ajax接收数据后,在成功方法中,使用jquery填充第二个下拉列表的值

function PopulateValueForSecondDropdown(data) {
    $('#DROPDOWN2').empty(); 
    data.forEach(function(item){
        $('#DROPDOWN2').append('<option value="' + item.KEY+ '">' + item.VALUE + '</option>');
    });
}

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

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