这是SO上的常见错误,但我似乎无法修复错误 传递到字典中的模型项的类型为' System.String',但此字典需要类型为' System.Collections.Generic.IEnumerable`1 [ITEye.ViewModels.IssueViewModel]的模型项&#39 ;.任何有关排除故障的帮助都将受到赞赏
视图模型
public class IssueViewModel
{
public int ItemId { get; set; }
public string Name { get; set; }
public string ItemLocation { get; set; }
public string UserName { get; set; }
}
控制器
public ActionResult Issueds()
{
var query = Issue();
return View(query);
}
public IEnumerable Issue()
{
var issued = from item in db.Items
join issues in db.Issues on item.ItemId equals issues.ItemId
join users in db.Staffs on issues.StaffId equals users.StaffId
where issues.StaffId == users.StaffId
select new IssueViewModel()
{
ItemId = item.ItemId,
Name = item.ItemName,
ItemLocation = item.Location.LocName,
UserName = users.StaffName
};
return issued.AsEnumerable();
}
查看
@model IEnumerable<ITEye.ViewModels.IssueViewModel>
@foreach (var item in Model) {
<p>
@item.Name
</p>
}
答案 0 :(得分:0)
尽管我已经确定代码没问题,因为我对此进行了广泛的研究,但问题在视图中却是错误的。我用@ {Html.RenderPartial(“Issueds”,“Issue”);}而不是@ {Html.RenderAction(“Issueds”,“Issue”);}
答案 1 :(得分:-1)
您的Issue
方法应该返回IEnumerable<ITEye.ViewModels.IssueViewModel>
而不是非泛型类型IEnumerable
public IEnumerable<ITEye.ViewModels.IssueViewModel> Issue()
{
var issued = from item in db.Items
join issues in db.Issues on item.ItemId equals issues.ItemId
join users in db.Staffs on issues.StaffId equals users.StaffId
where issues.StaffId == users.StaffId
select new IssueViewModel()
{
ItemId = item.ItemId,
Name = item.ItemName,
ItemLocation = item.Location.LocName,
UserName = users.StaffName
};
return issued;
}
答案 2 :(得分:-1)
在从Issue()
返回实体框架查询之前,您尚未实现该实体框架查询,因此它将作为SQL字符串传递给视图 - AsEnumerable()
将不执行任何操作,因为它已经是{{ 1}}。您需要具体化查询,因此请使用IEnumerable
之类的内容。
同时将方法的返回类型更改为通用:
ToList()