In model there are two tables. Books and Reviews of Books. Each book has some (or none) review with score point. In the Index controller action would like to pass list of books to view with additional column as average number of score points to that book.
Typically without additional column Index Body looks like this:
public ActionResult Index()
{
return View(db.Books.ToList());
}
and View use that model
@model IEnumerable<NameOfSolution.Models.Books>
What is the proper way to add more info? Build a different query like this(?) :
(without additional column):
public ActionResult Index()
{
var BooksList = from b in db.Books
select new
{
b.Id,
b.BookRef,
b.BookTitle,
b.Authors
};
// return View(db.Books.ToList());
return View(BooksList.ToList());
}
after that exception will be thrown:
The model item passed into the dictionary is of type System.Collections.Generic.List
1[<>f__AnonymousType14[System.Int32,System.Int32,System.String,System.String]]'
,
but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[NameOfSolution.Models.Books]'`.
The question is: what is the proper approach to solve this?
Should I do some cast var BooksList
to more generic model or add separate file class to solution and use it like strong typed model?