4
Answers

MVC Cascading dropdownlist

Partho Rocko

Partho Rocko

8y
227
1
My View is 
 
@model TryMVC.Models.MobileData   @{     ViewBag.Title = "SelectDistrict"; }    <script src="~/Scripts/jquery-1.7.1.min.js"></script>       <h2>Cascading Dropdownlist</h2>   <table>       <tr>           <td>               <label>State</label>           </td>           <td>   @Html.DropDownListFor(x => x.stateNamesModel, Model.stateNamesModel, "--Select--", new { @id="ddlState"});         </td>        </tr>       <tr>         <td>             <label>District</label>         </td>         <td id="District">             @Html.DropDownListFor(x => x.DistrictNamesModel, new List<SelectListItem>(), "--Select--", new { @id = "ddlDistrict" });         </td>     </tr>   </table>      <script type="text/javascript">     $(document).ready(function () {         $('#ddlState').change(function () {             $.ajax({                 type: "post",                 url: "/DropDownList/GetDistrict",                 data: { stateId: $('#ddlState').val() },                 datatype: "json",                 traditional: true,                 success: function (data) {                     var district = "<select id='ddlDistrict'>";                     district = district + '<option value="">--Select--</option>';                     for (var i = 0; i < data.length; i++) {                         district = district + '<option value=' + data[i].Value + '>' + data[i].Text + '</option>';                     }                     district = district + '</select>';                     $('#District').html(district);                 }             });         });     }); </script>    
 
My Controller Is 
 
   public ActionResult SelectDistrict()         {             MobileData md = new MobileData();             DBData dd = new DBData();             DataSet ds = new DataSet();             ds = dd.getState();             List<SelectListItem> stateNames = new List<SelectListItem>();                            foreach (DataRow item in ds.Tables[0].Rows)             {                 stateNames.Add(new SelectListItem { Text = item["StateName"].ToString() ,Value=(item["StateId"].ToString()) });             }             md.stateNamesModel = stateNames;             return View(md);         }           [HttpPost]         public ActionResult GetDistrict(string stateId)         {             MobileData md = new MobileData();             int statId;             List<SelectListItem> DistrictNames = new List<SelectListItem>();             if (!string.IsNullOrEmpty(stateId))             {                 statId = Convert.ToInt32(stateId);                 DBData dd = new DBData();                 DataSet ds = new DataSet();                 ds = dd.GetCityOnstate(statId);                                            foreach (DataRow item in ds.Tables[0].Rows)             {                 DistrictNames.Add(new SelectListItem { Text = item["City"].ToString() , Value=(item["CityId"].ToString()) });             }                     }             return Json(DistrictNames, JsonRequestBehavior.AllowGet);         }   
My Model Is
 
 
 public class MobileData     {            public IList<SelectListItem> stateNamesModel { get; set; }             public IList<SelectListItem> DistrictNamesModel { get; set; }             }   
 
 
My problem is , The dropdownlist for State is populated but [HttpPost] event for  GetDistrict is not Firing....Help me plz
GetDistrict 
Answers (4)
0
Bikesh Srivastava

Bikesh Srivastava

NA 19.8k 835.1k 8y
check it..
https://bikeshsrivastava.blogspot.in/2016/06/part-18how-to-implement-cascading.html
0
Partho Rocko

Partho Rocko

NA 10 806 8y
well organised code is here
My View is
@model TryMVC.Models.MobileData
 @{     ViewBag.Title = "SelectDistrict"; } 
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
 <h2>Cascading Dropdownlist</h2>
 <table>
 <tr> 
<td> <label>State</label> </td>
 <td> @Html.DropDownListFor(x => x.stateNamesModel, Model.stateNamesModel, "--Select--", new { @id="ddlState"});         </td>
 </tr>
 <tr> 
<td> <label>District</label> </td>
 <td id="District"> @Html.DropDownListFor(x => x.DistrictNamesModel, new List<SelectListItem>(), "--Select--", new { @id = "ddlDistrict" });         </td>
 </tr>
 </table>
<script type="text/javascript">  
   $(document).ready(function () {    
     $('#ddlState').change(function () {   
          $.ajax({   type: "post",
  url: "/DropDownList/GetDistrict", 
data: { stateId: $('#ddlState').val() },   
datatype: "json", 
 traditional: true, 
success: function (data) {  
var district = "<select id='ddlDistrict'>"; 
district = district + '<option value="">--Select--</option>'; 
  for (var i = 0; i < data.length; i++) { 
 district = district + '<option value=' + data[i].Value + '>' + data[i].Text + '</option>';     
}  '
district = district + '</select>'; 
$('#District').html(district); 
 } 
}); 
});
 }); </script>

My Controller Is
 public ActionResult SelectDistrict()   
   {    MobileData md = new MobileData();  
           DBData dd = new DBData();       
      DataSet ds = new DataSet();       
      ds = dd.getState();       
      List<SelectListItem> stateNames = new List<SelectListItem>(); 
     foreach (DataRow item in ds.Tables[0].Rows) 
 {  
 stateNames.Add(new SelectListItem { Text = item["StateName"].ToString() ,Value=(item["StateId"].ToString()) });   
 }   
  md.stateNamesModel = stateNames;
 return View(md);  
  } 
  [HttpPost]  
  public ActionResult GetDistrict(string stateId) 
{ 
 MobileData md = new MobileData();
 int statId; 
 List<SelectListItem> DistrictNames = new List<SelectListItem>();
 if (!string.IsNullOrEmpty(stateId)) 
  {   
 statId = Convert.ToInt32(stateId); 
  DBData dd = new DBData(); 
 DataSet ds = new DataSet();
  ds = dd.GetCityOnstate(statId); 
 foreach (DataRow item in ds.Tables[0].Rows) 
 { 
 DistrictNames.Add(new SelectListItem { Text = item["City"].ToString() , Value=(item["CityId"].ToString()) }); 
   }
 }  
 return Json(DistrictNames, JsonRequestBehavior.AllowGet);    
     }   
My Model Is
 public class MobileData   
  { 
  public IList<SelectListItem> stateNamesModel { get; set; } 
  public IList<SelectListItem> DistrictNamesModel { get; set; }      
       }   
My problem is , The dropdownlist for State is populated but [HttpPost] event for GetDistrict is not Firing....Help me plz
GetDistrict 
0
Partho Rocko

Partho Rocko

NA 10 806 8y
Yes i debugged it... But after reaching to<pre style="font-family: Consolas; font-size: 13px; background: white;"> <span style="background:yellow;">@</span>Html.DropDownListFor(x =&gt; x.DistrictNamesModel, <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SelectListItem</span>&gt;(), <span style="color:#a31515;">"--Select--"</span>, <span style="color:blue;">new</span> { @id = <span style="color:#a31515;">"ddlDistrict"</span> }); </pre><pre style="font-family: Consolas; font-size: 13px; background: white;">i get redirected to webpage to choose State and after selecting State , i am not able to jump on HTTPPostback method</pre>
0
Midhun T P

Midhun T P

NA 19.7k 281.1k 8y
Hi,
Have you tried debugging the code?
Is this jquery function is firing?$('#ddlState').change(function () {