我即将在 MVC 3 中创建级联下拉列表。Logik 很简单,每个位置可能有很多员工,所以我能弄清楚如何实现它的最佳方法是下拉列表。
我现在拥有的是:
1 个列表一开始就填满了位置。并且在更改时,我可以将数据获取到我的第二个员工列表中,但我传输的数据是可枚举日期”。
当我尝试向员工发送 ID 和姓名时,什么也没有发生。
Controller 代码:
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>
最佳答案
我可以看到的一件事是,您的操作中的变量名称以大写字母开头,并且 JavaScript 区分大小写。在您的页面中,您使用的是小写字母,但如果您想将大小写保留在 Controller 中,则这两个字母应该为大写字母。
关于jquery - 级联下拉菜单 MVC 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640196/