级联下拉列表MVC 3

我即将在MVC 3中创建级联下拉列表.Logik很简单,对于每个位置可能有很多Employees,因此我可以找出如何实现它的最佳方法是下拉列表。

我现在拥有的是:

1列表填写在与地点相关的地方。 在变化时,我可以将数据输入到我的第二个员工列表中,但我传输的数据是“可记录日期”。

一旦我尝试发送带有ID和名字的员工,没有任何事情发生。

控制器代码:

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); } 

查看:

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

使用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($('', { value: employee.value, text: employee.text })); }); }); } });  

我可以看到的一件事是,您的操作中的变量名称以大写字母开头,JavaScript区分大小写。 在您的页面中,您使用的是小写但如果要将shell保留在控制器中,则它们应为大写。