传递到字典中的模型项的类型为“System.Data.Linq.DataQuery`1[Models.MailListViewModel]”;
我收到一条错误消息:
传递到字典中的模型项的类型为“System.Data.Linq.DataQuery`1[WebUI.Models.MailListViewModel]”,但此字典需要类型为“WebUI.Models.MailListViewModel”的模型项。< /p>
这是在我的控制器中:
public ViewResult List()
{ var mailToShow = from m in mailRepository.Mail
join p in profilesRepository.Profiles on m.SenderId equals p.ProfileId
select new MailListViewModel
{
SenderId = m.SenderId,
ProfileId = p.ProfileId,
UserName = p.UserName,
Subject = m.Subject
};
return View(mailToShow);
}
这是在我的 MailListViewModel 中:
public class MailListViewModel
{
public int SenderId;
public int ProfileId;
public string UserName;
public string Subject;
}
这是在我的视图中:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.Models.MailListViewModel>" %>
<% foreach (var m in (IEnumerable)Model)
{ %>
<tr>
<td>
</td>
<td>TODO image</td>
<td>
<%: Model.SenderId %></td>
<td><%: Model.Subject%></td>
</tr>
<% } %>
错误消息似乎是冲突的,说我正在使用正确的模型。
我应该怎么办?
I am getting an error saying:
The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[WebUI.Models.MailListViewModel]', but this dictionary requires a model item of type 'WebUI.Models.MailListViewModel'.
This is in my controller:
public ViewResult List()
{ var mailToShow = from m in mailRepository.Mail
join p in profilesRepository.Profiles on m.SenderId equals p.ProfileId
select new MailListViewModel
{
SenderId = m.SenderId,
ProfileId = p.ProfileId,
UserName = p.UserName,
Subject = m.Subject
};
return View(mailToShow);
}
This is in my MailListViewModel:
public class MailListViewModel
{
public int SenderId;
public int ProfileId;
public string UserName;
public string Subject;
}
This is in my view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.Models.MailListViewModel>" %>
<% foreach (var m in (IEnumerable)Model)
{ %>
<tr>
<td>
</td>
<td>TODO image</td>
<td>
<%: Model.SenderId %></td>
<td><%: Model.Subject%></td>
</tr>
<% } %>
The error message seems to be conflicting, saying that I am using the correct model.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询返回一个
IQueryable
,本质上查询中的每个结果都有一个MailListViewModel
。您的视图只需要一个MailListViewModel
。您应该更新您的视图以期望
IEnumerable
,如下所示:此外,一旦完成,您就不应该将
Model
转换为IEnumerable< /code> 因为这将导致枚举中的每个项目被视为
object
类型,因此它看起来就像您的属性都不存在一样。Your query is returning an
IQueryable<MailListViewModel>
, essentially oneMailListViewModel
for every result in the query. Your view is expecting only a singleMailListViewModel
.You should update your view to expect
IEnumerable<MailListViewModel>
, like so:Also, once you've done that you shouldn't cast the
Model
toIEnumerable
as that will result in each item in the enumeration being treated as typeobject
so it will appear as though none of your properties exist.