我可以看到这已被问过几次,但是看不到它与存储过程调用有关,就像下面我可以工作一样。
public ActionResult _NewEmpFifth()
{
IEnumerable<SelectListItem> departments = new List<SelectListItem>();
using (EIPInternalEntities ctx = new EIPInternalEntities())
{
departments = new SelectList(ctx.Database.SqlQuery<string>("EXEC dbo.uspGetDepartments").ToList(), "DepartmentID", "Department");
ViewBag.Department = new SelectList(departments, dataValueField: "DepartmentID", dataTextField: "Department");
}
var sessionValues = Session["MySessionValues"] as MySessionValues;
return PartialView();
}
我知道问题是存储过程正在返回DepartmentId
和Department
这就是我想要的。我可以在这个方法下将那些文本绑定到value属性吗?
我还不熟悉框架和MVC。
答案 0 :(得分:0)
想出来。问题是,并且需要替换为与存储过程的返回匹配的模型。
新模型
public class GetDepartment
{
public int DepartmentID { get; set; }
public string Department { get; set; }
}
更改了控制器操作
public ActionResult _NewEmpFifth()
{
IEnumerable<SelectListItem> departments = new List<SelectListItem>();
using (EIPInternalEntities ctx = new EIPInternalEntities())
{
ViewBag.Department = new SelectList(ctx.Database.SqlQuery<GetDepartment>("EXEC dbo.uspGetDepartments").ToList(), "DepartmentID", "Department");
}
var sessionValues = Session["MySessionValues"] as MySessionValues;
return PartialView();
}