我正在开发一个ASP.net MVC项目,并希望显示一个特定的用户数据,我通过会话获得该数据。 根据该ID,我想提取特定员工的所有数据。 因为我做了这个代码:
我的控制器的索引方法:
public ActionResult Index()
{
object s = Session["EmployeeID"];
var sessval = s.ToString();
AdminDetailsModel model = new AdminDetailsModel();
var data1 = (from e in db.Employees.Where(c => c.EmployeeID == sessval) join d in db.Designations on e.Designation1up equals d.Designation1up select new { EmployeeID = e.EmployeeID, FirstName = e.FirstName, LastName = e.LastName, DesignationInternal = d.DesignationInternal, DesignationExternal = d.DesignationExternal, OfficePhone = e.OfficePhone, CellPhone = e.CellPhone, JoiningDate = e.JoiningDate, EmailID = e.EmailID, Address = e.Address }).SingleOrDefault();
return View(data1);
}
我为此创建了viewmodel,其中我采用了我需要在视图中显示的所有方法。
我已经创建了关于此方法的视图,该方法在我的模型类上是强类型的。 当我运行它时,我得到错误:
System.InvalidOperationException: The model item passed into the dictionary is of type '<>f__AnonymousType1`10[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.DateTime,System.String,System.String]', but this dictionary requires a model item of type 'ResourceTracking.Employee'.
我现在该怎么办?
答案 0 :(得分:1)
更改
select new {
到
select new ResourceTracking.Employee {
您希望将Employee
类的实例传递给视图而不是匿名类型。