Ayush Patil

Ayush Patil

  • 1.5k
  • 178
  • 19.6k

how to ajax call change event on dropdown in mvc

Apr 4 2018 4:32 AM
I trying to bind dropdown value from ZoneMaster To StateMaster but failed to bind value plz help me to solve.
 
view code:-
  1. <label class="control-label col-sm-2">Zone<span class="mandatory"></span>:</label>  
  2. @Html.DropDownList("ZoneMaster"null"---Select Zone----")  
  3. <br />  
  4. <br />  
  5. <label class="control-label col-sm-2">State<span class="mandatory"></span>:</label>  
  6. <select id="StateMaster" class="form-control input-md" ></select>  
  7. <br />  
  8. <br />  
  9. <label class="control-label col-sm-2">Branch<span class="mandatory"></span>:</label>  
  10. <select id="BranchMaster" class="form-control input-md" ></select>  
  11. <script>  
  12. $(document).ready(function () {  
  13. $("#ZoneMaster").change(function () {  
  14. var id = $(this).val();  
  15. $("#StateMaster").empty();  
  16. $.get("StateMaster_Bind", { ZoneName: ZoneMaster }, function (data) {  
  17. var v = "<option>---Select---</option>";  
  18. $.each(data, function (i, v1) {  
  19. v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";  
  20. });  
  21. $("#StateMaster").html(v);  
  22. });  
  23. });  
  24. $("#StateMaster").change(function () {  
  25. var id = $(this).val();  
  26. $("#BranchMaster").empty();  
  27. $.get("BranchMaster_Bind", { StateCode: StateMaster}, function (data) {  
  28. var v = "<option>---Select---</option>";  
  29. $.each(data, function (i, v1) {  
  30. v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";  
  31. });  
  32. $("#BranchMaster").html(v);  
  33. });  
  34. });  
  35. });  
  36. </script>  
ModelCode:-
  1. public class CreateUserView  
  2. {  
  3. public string Code { getset; }  
  4. public string FirstName { getset; }  
  5. public string LastName { getset; }  
  6. public int Role { getset; }  
  7. public string EmailID { getset; }  
  8. public int MobileNo { getset; }  
  9. public string ZoneMaster { getset; }  
  10. public string StateMaster { getset; }  
  11. public string BranchMaster { getset; }  
  12. public string CompanyType { getset; }  
  13. }  
ControllerCode:-
  1. public void ZoneMaster_Bind()  
  2. {  
  3. DataSet ds = dblayer.Get_Zone();  
  4. List<SelectListItem> ZoneMasterlist = new List<SelectListItem>();  
  5. foreach (DataRow dr in ds.Tables[0].Rows)  
  6. {  
  7. ZoneMasterlist.Add(new SelectListItem { Text = dr["ZoneName"].ToString(), Value = dr["ZoneName"].ToString() });  
  8. }  
  9. ViewBag.ZoneMaster = ZoneMasterlist;  
  10. }  
  11. public JsonResult StateMaster_Bind(string ZoneCode)  
  12. {  
  13. DataSet ds = dblayer.Get_State(ZoneCode);  
  14. List<SelectListItem> StateMasterlist = new List<SelectListItem>();  
  15. foreach (DataRow dr in ds.Tables[0].Rows)  
  16. {  
  17. StateMasterlist.Add(new SelectListItem { Text = dr["State"].ToString(), Value = dr["StateCode"].ToString() });  
  18. }  
  19. return Json(StateMasterlist, JsonRequestBehavior.AllowGet);  
  20. }  
  21. public JsonResult BranchMaster_Bind(string StateCode)  
  22. {  
  23. DataSet ds = dblayer.Get_Branch(StateCode);  
  24. List<SelectListItem> BranchMasterList = new List<SelectListItem>();  
  25. foreach (DataRow dr in ds.Tables[0].Rows)  
  26. {  
  27. BranchMasterList.Add(new SelectListItem { Text = dr["Branch"].ToString(), Value = dr["BranchCode"].ToString() });  
  28. }  
  29. return Json(BranchMasterList, JsonRequestBehavior.AllowGet);  
  30. }

Answers (1)