I am making a small ASP.NET MVC 3 test application with a bookshelf where I can list "Books" and loan and return them to / from "Loaners".
I want to show both my books and my Loaners in one view, and for this I created a ViewModel called BooksViewModel, but I can’t understand why I can’t list my "Books". I can do “foreach (var item in Model)” and just touch the “Book” inside it, none of the properties of the “Book”.
My model
namespace MyTest.Models {
My controller:
BookshelfDb bookshelf = new BookshelfDb(); public ActionResult Index() { var books = from book in bookshelf.Books select book; var loaners = from loaner in bookshelf.Loaner select loaner; var bookViewModel = new BooksViewModel(books.ToList(),loaners.ToList()); return View(bookViewModel); }
My viewmodel
public class BooksViewModel { public BooksViewModel(List<Book> books, List<Loaner> loaners) { this.Books = books; this.Loaners = loaners; } public List<Book> Books { get; private set; } public List<Loaner> Loaners { get; private set; } }
My view
@model IEnumerable<MyTest.ViewModels.BooksViewModel> @foreach (var item in Model.Books) { @item.Title }
Any hints or code fixes would be greatly appreciated!
source share