dimanche 20 avril 2014

Linq GroupBy anonymous type error MVC4


Vote count:

0




I have the code below that uses group to selects distinct values. Intellisence and code break shows the query is working as expected.



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);
}


The problem is the razor view which gives the following error The model item passed into the dictionary is of type: 'System.Collections.Generic.List1[<>f__AnonymousType193[System.Int32,System.Int32,System.Int32]]', but this dictionary requires a model item of type '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>


I will be grateful if anyone could point me to the right direction





asked 16 mins ago

Diin

126





Aucun commentaire:

Enregistrer un commentaire