当我尝试从JQuery获取“值”以根据下拉列表中的用户选择绑定来自2个不同表的ID时,我在Controller中使用LINQ获得了“上下文”中的错误
[HttpPost]
public ActionResult GetEmpNo(string value)
{
string str = value;
List<SelectListItem> items = new List<SelectListItem>();
if (value == "Representative")
{
// query the tb_RepDetails table and get values.
List<SelectListItem> list = new List<SelectListItem>();
list = context.tb_RepDetails.Select(c => new SelectListItem() { Text = c.RepId }).ToList();
}
else if (value == "Agent")
{
//query the tb_AgentDetails table and get values.
List<SelectListItem> list = new List<SelectListItem>();
list = context.tb_AgentDetails.Select(c => new SelectListItem() { Text = c.AgentId }).ToList();
}
return Json(items, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:1)
在开始时,您声明items
,然后设置list
变量,您就不会返回任何内容。
应该是:
[HttpPost]
public ActionResult GetEmpNo(string value)
{
string str = value;
List<SelectListItem> items = new List<SelectListItem>();
if (value == "Representative")
{
// query the tb_RepDetails table and get values.
items = context.tb_RepDetails.Select(c => new SelectListItem() { Text = c.RepId }).ToList();
}
else if (value == "Agent")
{
//query the tb_AgentDetails table and get values.
items = context.tb_AgentDetails.Select(c => new SelectListItem() { Text = c.AgentId }).ToList();
}
return Json(items, JsonRequestBehavior.AllowGet);
}