【发布时间】:2014-04-20 21:26:13
【问题描述】:
我有下面的代码,它使用组来选择不同的值。智能和代码中断显示查询按预期工作。
public ActionResult _PortfoliosbyCountry()
{
var portfolios = db.Portfolios;
var query = (from t in portfolios
group t by new { t.UserId, t.CountryId,t.PortfolioTypeId }
into grp
select new
{
grp.Key.CountryId,
grp.Key.PortfolioTypeId,
grp.Key.UserId
}).ToList();
return PartialView(query);
}
型号
namespace RefST.Models
{
public class Portfolio
{
[Key]
public int PId { get; set; }
[Required]
public string Title { get; set; }
[ScaffoldColumn(false)]
public DateTime DatePosted { get; set; }
[ScaffoldColumn(false)]
public DateTime LastEdited { get; set; }
[AllowHtml][Required]
public string Body { get; set; }
public int UserId {get; set; }
public int CountryId { get; set; }
public int PortfolioTypeId { get; set; }
public virtual Country Country { get; set; }
public virtual PortfolioType PortfolioType { get; set; }
}
}
问题是剃刀视图,它给出了以下错误
传入字典的模型项的类型是:'System.Collections.Generic.List1[<>f__AnonymousType193[System.Int32,System.Int32,System.Int32]]',但是这个字典需要一个模型项输入“System.Collections.Generic.IEnumerable`1[RefST.Models.Portfolio]”。
@model IEnumerable<RefST.Models.Portfolio>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.CountryId)
</th>
<th>
@Html.DisplayNameFor(model => model.PortfolioTypeId)
</th>
<th></th>
</tr>
@foreach (var b in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => b.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => b.CountryId)
</td>
<td>
@Html.DisplayFor(modelItem => b.PortfolioTypeId)
</td>
</tr>
}
</table>
如果有人能指出正确的方向,我将不胜感激
【问题讨论】:
-
你能显示你的
PortFolio模型的代码吗? -
报错是说你是匿名分组,select new 后添加 Portfolio。所以选择新的投资组合{grp.key。等}。
标签: c# asp.net-mvc linq razor