I have the following Action in Controller:
public ViewResult Index()
{
//select
var query = (from u in db.UserSets
join f in db.FriendRequestsSets on u.ID equals f.FriendID
orderby u.ID
select new {Id = u.ID, Nickname = u.Nickname}).AsEnumerable();
ViewBag.FriendRequests = query;
return View(this.UserColl());
}
The following foreach in my Razor view:
<ul>
@foreach (var item in ViewBag.FriendRequests)
{
<li>@item.Nickname</li>
}
</ul>
And the following error:
*'object' does not contain a definition for 'Nickname'*
What am I missing in my code?
ViewBag is of type object, not an Enumerable!
What you can do, is to have a class as follow:
then populate an list of User class as bellow:
and at last in your view do this:
Hope it helps.