Theolin Naidoo

Theolin Naidoo

  • NA
  • 3
  • 650

Drop Down List Giving Incorrect Values

Mar 31 2017 4:41 AM

Hi everyone,

I have followed the following tutorial for my project:

https://www.youtube.com/watch?v=h_ViuyVs4AE

The issue that I have is that when I click an item in the first drop down box, it does not give the correct values in the second box, it only gives one value and it is incorrect. It should display all the Model_Name's(Vehicle_Model table) that are for each Make_Name(Vehicle_Make table).

Index.cshtml code:
 
  1. @model GoogleMap.Models.MyPageViewModel  
  2.   
  3.   
  4.   
  5.             <script>  
  6.                 $(function () {  
  7.                     $("#ContID").change(function () {  
  8.                         $.get("/Map/GetModById", { MID: $("#ContID").val() }, function (data) {  
  9.                             $("#St").empty();  
  10.                             $.each(data, function (index, row) {  
  11.                                 $("#St").append(" <option value='" + row.Model_ID + "'>" + row.Model_Name + "</option>")  
  12.   
  13.                             });  
  14.                         })  
  15.                     });  
  16.                 });  
  17.   
  18.             </script>  
  19.             @Html.DropDownListFor(p => p.SelectedMake_Id, ViewBag.Vehicle_Make as SelectList, "Select Vehicle Make", new { id = "ContID" })  
 MapController.cs code:
 
  1. public class MapController : Controller  
  2.     {  
  3.   
  4.         private GoogleMapEntities db = new GoogleMapEntities();  
  5.         //Get the select ID???  
  6.         int SelectedMake_Id = 0;  
  7.                 
  8.   
  9.         // GET: Map  
  10.         public ActionResult Index()  
  11.         {      
  12.             GoogleMapEntities GE = new GoogleMapEntities();  
  13.             List<Vehicle_Details> vehList = db.Vehicle_Details.ToList();  
  14.   
  15.             GoogleMapViewModel GMviewmodel = new GoogleMapViewModel();  
  16.             List<GoogleMapViewModel> GMviewmodelList = new List<GoogleMapViewModel>();  
  17.   
  18.   
  19.             //Populate the ViewModel  
  20.             MyPageViewModel vm = new Models.MyPageViewModel();  
  21.             vm.GoogleMapViewModelList = GMviewmodelList;  
  22.             vm.SelectedMake_Id = SelectedMake_Id;  
  23.        
  24.   
  25.             ViewBag.Vehicle_Make = new SelectList(db.Vehicle_Make, "Make_ID""Make_Name");  
  26.               
  27.             return View(vm);                 
  28.         }  
  29.   
  30.         public JsonResult GetModById(int MID)  
  31.         {  
  32.             db.Configuration.ProxyCreationEnabled = false;  
  33.             return Json(db.Vehicle_Model.Where(p => p.Model_ID == MID), JsonRequestBehavior.AllowGet);  
  34.         }  
  35.   
  36.         [HttpPost]  
  37.         public ActionResult Search(string Location)  
  38.         {  
  39.             GoogleMapEntities GE = new GoogleMapEntities();  
  40.             ////SELECT Make_Name DATA FROM DB1  
  41.             // var result = GE.Vehicle_Model.Where(x => x.Model_Name.StartsWith(Location)).ToList();  
  42.             var GetVeh = db.GetMapSearch().Where(x => x.Model_Name.StartsWith(Location)).ToList();  
  43.           
  44.             return Json(GetVeh, JsonRequestBehavior.AllowGet);  
  45.   
  46.         }  
  47.   
  48.     }  
 
 

Answers (1)