我即将在MVC 3中创建级联下拉列表。逻辑很简单,对于每个位置可能会有很多员工,所以我能想到的最好的实现方式是下拉列表。
我现在拥有的是:
1. 列表在开始时填充了位置数据。当更改位置时,我可以获取第二个员工列表中的数据,但是我转移的数据是“可枚举的日期”。
2. 一旦我尝试通过ID和名称发送员工信息,就没有任何反应。
控制器代码:
我现在拥有的是:
1. 列表在开始时填充了位置数据。当更改位置时,我可以获取第二个员工列表中的数据,但是我转移的数据是“可枚举的日期”。
2. 一旦我尝试通过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);
}
视图:
<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>