我是否可以在此列表中获得相同的 SelectListItems:
public static List<SelectListItem> GetAreaApprovingAuthorities(int id)
{
List<Employee> approvingAuthorities = new List<Employee>();
using (var db = new TLMS_DBContext())
{
approvingAuthorities = db.Employees.Where(e => e.UserRoleID > 1 && e.PersonnelAreaID == id).ToList();
}
List<SelectListItem> returned = new List<SelectListItem>();
foreach (Employee emp in approvingAuthorities)
{
returned.Add(new SelectListItem { Text = string.Format("{0} {1}", emp.FirstName, emp.LastName), Value = emp.ID.ToString() });
}
return returned;
}
并使用 Json 将它们传递到选择列表中?这是获取列表的控制器操作:
public JsonResult GetApprovingAuthorities(int id)
{
return Json(TLMS_DropDownLists.GetAreaApprovingAuthorities(id),JsonRequestBehavior.AllowGet);
}
这里是 json 对象被迭代然后传递给选择列表的地方(当另一个选择列表的值改变时触发):
$.ajax({
type: 'GET',
data: { id: selectedValue },
url: '@Url.Action("GetApprovingAuthorities")',
contentType: "application/json; charset=utf-8",
global: false,
async: false,
dataType: "json",
success: function (jsonObj) {
$('#aa').empty();
$.each(jsonObj, function (key, value) {
$('#aa').append($("<option/>", {
value: key.Text,
text: value.Text
}));
});
}
});
这正在填充“aa”选择列表,我正在通过控制器操作中的 FormCollection 接收选择列表的选定项目,但我无法从“GetAreaApprovingAuthorities”SelectListItem 的值中捕获原始 ID。有没有办法让我做到这一点?