JQuery
JQuery

Reputation: 905

The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types from a stored procedure

I can see this has been asked a few times but can't see one where it relates to a stored proc call like below i can work form.

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

I know the issue is the stored procedure is returning DepartmentId and Department which is what I want. Can I bind those to a text a value property under this method?

I'm still new to frame work and MVC.

Upvotes: 0

Views: 2091

Answers (1)

JQuery
JQuery

Reputation: 905

Figured it out. Issue was with the and needed to be replaced by a model that matched the return from the stored procedure.

New model

public class GetDepartment
{
    public int DepartmentID { get; set; }
    public string Department { get; set; }

}

changed controller action

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

Upvotes: 1

Related Questions