I want to pass two collections of objects. First is Post, second is Gallery. However I get error and I don’t know how to fix this.
I’ve done this when passing two single objects and it is working fine, but now I need to pass two collections of those objects and it gives me error.
Error
The model item passed into the dictionary is of type
‘System.Tuple2[System.Linq.IQueryable1[photoBlog.Models.Gallery],System.Linq.IQueryable1[photoBlog.Models.Post]]',2[System.Collections.Generic.IEnumerable
but this dictionary requires a model item of type
'System.Tuple1[photoBlog.Models.Gallery],System.Collections.Generic.IEnumerable1[photoBlog.Models.Post]]’.
Controller
public ActionResult Index()
{
photoBlogModelDataContext _db = new photoBlogModelDataContext();
var posts = _db.Posts.OrderByDescending(x => x.DateTime).Take(4);
var galleries = _db.Galleries.OrderByDescending(x => x.ID).Take(4);
return View(Tuple.Create(galleries, posts));
}
View
@model Tuple<IEnumerable<photoBlog.Models.Gallery>, IEnumerable<photoBlog.Models.Post>>
@foreach (var item in Model.Item1)
{
@item.Name
}
I think you should modify your controller method to this:
From your error message, it appears that the queries are not resolved yet when your view is rendered. By also doing ToArray or ToList, you will force the query to hit the database before you return from the controller method.