Cascade outputs MVC 3

I'm going to create cascading drop-down lists in MVC 3. Logik is simple, there can be many employees for each location, so the best way I could figure out how to implement it is down down lists.

what i mean now:

1 list is filled at startup using locations. and when I change, I can get the data in the second list of Employee, but the data transfer I are numbers that can be listed. "

As soon as I try to send employees with id, and the name nothing happens.

Controller Code:

public ActionResult Months(string locId) { var k = service.getEmployeeForLocation(Int32.Parse(locId)) .ToList() .Select(x => new { Value = .EmployeeId, Text = x.Name }); return Json(k,JsonRequestBehavior.AllowGet); } 

View:

 <tr> <td>Choose your closest location : @Html.DropDownListFor(x => x.SelectedLocation, Model.Locations)</td> <td>Choose your closest location : @Html.DropDownListFor(x => x.SelectedEmployee, Enumerable.Empty<SelectListItem>(), "-- select month --") </tr> 

Javascript

 </script> <script type="text/javascript"> $('#SelectedLocation').change(function () { var selectedLocation = $(this).val(); if (selectedLocation != null && selectedLocation != '') { $.getJSON('@Url.Action("Months")', { locId: selectedLocation }, function (employee) { var EmployeeSelect = $('#SelectedEmployee'); // monthsSelect.empty(); $.each(employee, function (index, employee) { EmployeeSelect.append($('<option/>', { value: employee.value, text: employee.text })); }); }); } }); </script> 
+4
source share
1 answer

One thing I see is that your variable names in your action begin with uppercase, and JavaScript is case sensitive. You use lowercase letters on your page, but they must be uppercase if you want to keep the case in your controller.

+2
source

Source: https://habr.com/ru/post/1388073/


All Articles