MVC里面两个dropdownlist联动

MVC view里面有两个dropdownlist分别邦定到两个属性


View里面

    <div class="row">
        <div class="col-md-4">
            @Html.LabelFor(m => m.Name, new { @class = "form-label" })
            @Html.DropDownListFor(m => m.Id, (IEnumerable<SelectListItem>)ViewData["DSPService"], "Please Select", new { @id="dropdowndsp", @class = "form-control" ,<span style="color:#ff0000;"> @οnchange="filterdropdownlist()"</span>} )
        </div>
        <div class="col-md-4">
            @Html.Label("DSP Service Detail")
            @Html.DropDownListFor(m => m.Id, (IEnumerable<SelectListItem>)ViewData["DSPServiceDetail"], "Please Select", new { @id="dropdowndspdetail" ,@class = "form-control" })
        </div>
    </div>


这里给第一个dropdownlist添加了onchange事件


在Controller里面

 public ActionResult Home()
        {

            Merret.Store.Domain.Tables.DeliveryServiceProvider mobj = new Merret.Store.Domain.Tables.DeliveryServiceProvider();
            mobj.Id = "DPD";
            mobj.Name = "DPD Service";

            SelectList  dsps =new SelectList (
               new List<SelectListItem>
               {
                    new SelectListItem {Text="DPD" ,Value="DPD" },
                    new SelectListItem {Text="META",Value="META" }
               },"Value","Text");


            List<DeliveryService> lstdspservice = new List<DeliveryService>
                {
                    new DeliveryService {Id="DPD", DSPId ="DPD",Description ="DPD1" },
                    new DeliveryService {Id="2", DSPId ="DPD",Description ="DPD2" },
                    new DeliveryService {Id="3", DSPId ="DPD",Description ="DPD3" },
                    new DeliveryService {Id="4", DSPId ="META",Description ="Meta1" },
                    new DeliveryService {Id="5", DSPId ="META",Description ="Meta2" },
                    new DeliveryService {Id="6", DSPId ="META",Description ="Meta3" },
                };
                
           

           <span style="color:#ff0000;"><strong> SelectList dspsdetail = new SelectList(
              lstdspservice , "Id", "Description", "DSPId", (object)mobj.Id);
</strong></span>
                ViewData["DSPService"] =dsps;
                ViewData["DSPServiceDetail"] = dspsdetail;


            return View("DisplayDSPs",mobj);
    
        }


给Dropdownlist  “dropdowndspdetail” 提供数据的selectlist里面第三个参数是datagroupfield


然后添加javascript, 就是在第一个dropdownlist的onchange事件中对第二个Dropdownlist的datagroupfield进行筛选

            function filterdropdownlist()
            {
                var d = $("#dropdowndsp");

               <strong> $("#dropdowndsp").filterSelectGroups("dropdowndspdetail");</strong>
            }





-

01-01
-
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值