on select change event - Html.DropDownListFor

后端 未结 3 420
野的像风
野的像风 2020-12-01 05:49

I have two dropdownlist. The selected value from the first one loads the other. How do I do that when I have the helper methods in a controller?

@using (Html         


        
3条回答
  •  悲&欢浪女
    2020-12-01 06:13

    Use the following code. It is used in my project. For Zone and Region I used two drop-down list. On change Zone data I loaded the Region drop-down.

    In View page

     @Html.DropDownList("ddlZone", new SelectList(@ViewBag.Zone, "Zone_Code", "Zone_Name"), "--Select--", new { @class = "LoginDropDown" })
    
     @Html.DropDownList("ddlRegion", Enumerable.Empty(), new { @class = "LoginDropDown" })
    

    The Zone need to load when the view page is load.

    In the controller write this method for Region Load

     [WebMethod]
            public JsonResult LoadRegion(string zoneCode)
            {
                ArrayList arl = new ArrayList();
    
                RASolarERPData objDal = new RASolarERPData();
                List region = new List();
    
                region = erpDal.RegionByZoneCode(zoneCode);
    
                foreach (tbl_Region rg in region)
                {
                    arl.Add(new { Value = rg.Reg_Code.ToString(), Display = rg.Reg_Name });
                }
    
                return new JsonResult { Data = arl };
            }
    

    Then use the following JavaScript

    
    

提交回复
热议问题