我在控制器中遇到错误 [英] I am getting error in controller
本文介绍了我在控制器中遇到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在那里做MVC项目我遇到了错误。请帮忙解决。
Hi All,
I am doing MVC project there I am getting below error. Please help to resolve.
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType1`5[System.Int32,System.String,System.String,System.DateTime,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[package.Models.EventsModel]'.
我的控制器代码是
My controller code is
public ActionResult Index()
{
var Q = from a in _ObjectEntities.Events
join b in _ObjectEntities.Employees on a.Incharge equals b.Id
select new {
EventId = a.EventId,
EventName = a.EventName,
EventType = a.EventType,
EventDate = a.EventDate,
Incharge = b.Name
};
return View(Q.AsEnumerable());
}
页眉是:
Page header is:
<%@ Page Title="Events" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ToPSCompilers.Models.EventsModel>>" %>
型号:
Model:
public class EventsModel
{
public int EventId { get; set; }
public String EventName { get; set; }
public String EventType { get; set; }
public DateTime EventDate { get; set; }
public String Incharge { get; set; }
}
我与Google核实但没有用。我在Visual Studio 2010中使用MVC2项目
I checked with Google but no use. I am using MVC2 project with Visual studio 2010
推荐答案
您的代码:
your code:
public ActionResult Index()
{
var Q = from a in _ObjectEntities.Events
join b in _ObjectEntities.Employees on a.Incharge equals b.Id
select new {
EventId = a.EventId,
EventName = a.EventName,
EventType = a.EventType,
EventDate = a.EventDate,
Incharge = b.Name
};
return View(Q.AsEnumerable());
}
更改为:
Change to this:
public ActionResult Index()
{
var Q = from a in _ObjectEntities.Events
join b in _ObjectEntities.Employees on a.Incharge equals b.Id
select new EventsModel {
EventId = a.EventId,
EventName = a.EventName,
EventType = a.EventType,
EventDate = a.EventDate,
Incharge = b.Name
};
return View(Q.AsEnumerable());
}
这篇关于我在控制器中遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文