简体   繁体   English

如何使用另一个下拉值填充下拉列表

[英]how to populate drop-down based on another drop-down value using

I have few drop-down list here as shown in 我在这里有一些下拉列表,如下所示 下图 , based on this drop-down selection next drop-down to it should be populated.. i tried to use this keyword to get value of current drop-down but i am unable to get it. ,基于此下拉选择,应该填充下一个下拉列表。.我试图使用此关键字来获取当前下拉列表的值,但我无法获取。

    <td class="border-top-0 border-left-0 border-right-0 align-middle form-group">
                  @{

                    SelectList newSelectList = new SelectList((from s in Model.UserMasterList
                         .ToList()
                                                               select new
                                                               {
                                                                   userId = s.userId,
                                                                   userName =  (s.userFirstName +' '+  s.userLastName)
                                                               }).Distinct()
                                                               ,
                         "userId",
                         "userName",
                         string.IsNullOrEmpty(item.JobConstructionManagerId.ToString()) ? 0 : item.JobConstructionManagerId);           
                                    }



                  @Html.DropDownListFor(model => item.JobConstructionManagerId, (SelectList)newSelectList, new { @class = "form-control js-select js-noFilter hidden DDConstructionManager", size = "2", @value = 0, Id = "JobConstructionManager" + t ,@OnChange="fill();"}) //first dropdown



                   </td>
             <td class="border-top-0 border-left-0 border-right-0 text-center text-align-center"> 

                   @{

                    SelectList newSelectStaffList = new SelectList((from s in Model.UserMasterStaffList                    //.UserConstructionManagersDetailList
                         .ToList()
                                                               select new
                                                               {
                                                                   SuserId  =  s.userId,                                    //s.conUserId,
                                                                   SuserName = (s.userFirstName + ' ' + s.userLastName)    //(s.mqUserMaster.userFirstName +' '+  s.mqUserMaster.userLastName)
                                                               }).Distinct()
                                                               ,
                         "SuserId",
                         "SuserName",
                         string.IsNullOrEmpty(item.JobStaffId.ToString()) ? 0 : item.JobStaffId);           
                                    }


                  @Html.DropDownListFor(model => item.JobStaffId, (SelectList)newSelectStaffList, new { @class = "form-control js-select js-noFilter hidden DDStaff", size = "2", @value = 0, Id = "JobStaff" + t }) //second dropdown

             </td>

main problem is that how to get just next drop-down to some particular drop-down 主要问题是如何将下一个下拉菜单转换为某些特定的下拉菜单

You must give an id attribute to your first dropdownlist then handle change event of dropdown with jquery to populate second dropdown. 您必须为第一个下拉列表提供id属性,然后使用jquery处理下拉列表的change事件以填充第二个下拉列表。

<script type="text/javascript">

            $('#firstDropDownId').change(function () {
                $(function () {
                    $.ajax({
                        url: '@Url.Action("GetSecondData", "YourController")',
                        type: 'POST',
                        dataType: 'json',
                        data: { 'firstData': $("#firstDropDownId").val() },
                        success: function (data) {
                            var options = $('#secondDropDownId');
                            options.empty();
                            $.each(data, function (i, item) {
                                options.append($('<option />').val(item.Id).text(item.Display));
                            });
                        },
                        error: function (response) {
                        }
                    });

                });
            });
        });

</script>

and then create an action method in your controller to populate second dropdown and return in json format. 然后在您的控制器中创建一个操作方法,以填充第二个下拉列表并以json格式返回。

[HttpPost]
public JsonResult GetSecondData(int firstId)
{
     var result = ...; //populate result   
     return new JsonResult { Data = result };
}

I am giving you the country state example you can use this concept 我给你国家的例子,你可以使用这个概念

<select name="country" id="country" onchange="states('state')">


<option value="">Select Country</option>
    <option value="1">Afghanistan</option>
    <option value="2">Albania</option>
    <option value="3">Algeria</option>
</select>

<select name="state" id="state">
<option value="">Select State</option>

function states(target){ var country = $("#country option:selected").val(); 功能状态(目标){var country = $(“#country option:selected”)。val(); $.ajax({ type: "GET", url: "url/"+country, dataType: "text", success: function(data){ if(data !=''){ $('#'+).html(data); } } }); $ .ajax({type:“ GET”,url:“ url /” + country,dataType:“ text”,成功:function(data){if(data!=''){$('#'+)。 html(data);}}}); } }

In your first dropdown list, add another data-* attribute "cascade-list-id". 在第一个下拉列表中,添加另一个data- *属性“ cascade-list-id”。

@Html.DropDownListFor(model => item.JobConstructionManagerId, (SelectList)newSelectList, new { @class = "form-control js-select js-noFilter hidden DDConstructionManager", size = "2", @value = 0, Id = "JobConstructionManager" + t ,@OnChange="fill();" "data-cascade-list-id"="newSelectStaffList" + t}) //first dropdown

In fill method, get the cascase list id, bind the new data with the id reference. fill方法中,获取书包列表ID,并将新数据与ID引用绑定。

// pseudo code
function fill() {
    var _that = this;
    var cascadeId = $(_that).attr("data-cascade-list-id") // or use .data("cascadeListId");

    // code to get the new data and binding, omitted for brevity
}

Hope this helps you.. 希望这对您有帮助。

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

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