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'); </script>
source share