jeff banjo

jeff banjo

  • NA
  • 25
  • 529

How to get dropdown based on another dropdown MVC(VB.Net)

Apr 2 2018 12:31 PM
I went through this tutorial and converted it for my VB.Net use,
 
https://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/
 
However, I get an error  [object Object], I have tried all i could to debug but no headway, am not a javascript fan, i came from webform and never had to write any scripts, Please can anyone help as i am already stock on the project and am over 93% completion! just this dropdown stuff, are there any other alternatives too? Thanks in advance. Here is my code
 
Controller
  1. Public Function PullStates(ByVal id As IntegerAs JsonResult  
  2.   
  3.             Dim states = db.States.Where(Function(s) s.CountryCode = id).[Select](Function(s) New With   
  4.            {  
  5.             Key .Text = s.Descrip,   
  6.             Key .Value = s.Code  
  7.             })  
  8.             Return Json(New SelectList(states, "Value""Text"))  
  9.   
  10.         End Function  
  1. 'GET: Region/Create  
  2.         Function Create() As ActionResult  
  3.             ViewBag.Code = "AUTO"  
  4.   
  5.   
  6.             ViewBag.Country = db.Countries.[Select](Function(c) New SelectListItem With  
  7.             {  
  8.                 .Text = c.Descrip,  
  9.                 .Value = c.CountryCode  
  10.             })  
  11.   
  12.               
  13.             Return View()  
  14.         End Function  
View
  1. @Html.DropDownList("Country", CType(ViewBag.Country, IEnumerable(Of SelectListItem)), "--Select Country--", htmlAttributes:=New With {.class = "tb8"})  
  1. @Html.DropDownList("State", New SelectList(String.Empty, "Value", "Text"), "--Please Select State--", htmlAttributes:=New With {.class = "tb8"})  
Script
  1. <script src="~/Scripts/ddl/jquery-1.7.1.js" type="text/javascript"></script>    
  2. <script src="~/Scripts/ddl/jquery-1.7.1.min.js" type="text/javascript"></script>    
  3. <script type="text/javascript">    
  4. $(document).ready(function () {    
  5.         //Dropdownlist Selectedchange event    
  6.         $("#Country").change(function () {    
  7.             $("#State").empty();    
  8.             $.ajax({    
  9.                 type: 'POST',    
  10.                 url: '@Url.Action("PullStates")'// we are calling json method    
  11.                 dataType: 'json',    
  12.                 data: { id: $("#Country").val() },    
  13.                 success: function (states) {    
  14.                     // states contains the JSON formatted list    
  15.                     // of states passed from the controller    
  16.                     $.each(states, function (i, state) {    
  17.                         $("#State").append('<option value="' + state.Value + '">' + state.Text + '</option>');    
  18.                     }); // here we are adding option for States    
  19.                 },    
  20.                 error: function (ex) {    
  21.                     alert('Failed to retrieve states.' + ex);    
  22.                 }    
  23.             });    
  24.             return false;    
  25.         })    
  26.     });    
  27. </script>

Answers (1)