数据阅读器有多个字段。多个字段对于存储过程中的EDM原语或枚举类型无效

时间:2015-04-21 10:16:09

标签: c# entity-framework

我可以看到这已被问过几次,但是看不到它与存储过程调用有关,就像下面我可以工作一样。

 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();
    }

我知道问题是存储过程正在返回DepartmentIdDepartment这就是我想要的。我可以在这个方法下将那些文本绑定到value属性吗?

我还不熟悉框架和MVC。

1 个答案:

答案 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();
}