MVC里面两个dropdownlist联动

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


View里面

    
@Html.LabelFor(m => m.Name, new { @class = "form-label" }) @Html.DropDownListFor(m => m.Id, (IEnumerable)ViewData["DSPService"], "Please Select", new { @id="dropdowndsp", @class = "form-control" , @οnchange="filterdropdownlist()"} )
@Html.Label("DSP Service Detail") @Html.DropDownListFor(m => m.Id, (IEnumerable)ViewData["DSPServiceDetail"], "Please Select", new { @id="dropdowndspdetail" ,@class = "form-control" })


这里给第一个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
               {
                    new SelectListItem {Text="DPD" ,Value="DPD" },
                    new SelectListItem {Text="META",Value="META" }
               },"Value","Text");


            List lstdspservice = new List
                {
                    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" },
                };
                
           

            SelectList dspsdetail = new SelectList(
              lstdspservice , "Id", "Description", "DSPId", (object)mobj.Id);

                ViewData["DSPService"] =dsps;
                ViewData["DSPServiceDetail"] = dspsdetail;


            return View("DisplayDSPs",mobj);
    
        }


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


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

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

                $("#dropdowndsp").filterSelectGroups("dropdowndspdetail");
            }





你可能感兴趣的:(有用的技术)