Reputation: 1063
This is my repository:
namespace Repositories
{
public class AuthorRepository : IAuthorRepository
{
AppContext myDB = new AppContext();
public List<Author> GetAllFromRepo()
{
return myDB.Authors.ToList();
}
}
}
My service file:
namespace Services
{
public class AuthorService : IAuthorService
{
private readonly IAuthorRepository _AuthorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
_AuthorRepository = authorRepository;
}
public List<AuthorViewModel> GetAll()
{
List<Author> authors = _AuthorRepository.GetAllFromRepo();
return authors.Select( x => new AuthorViewModel()
{
ID = x.ID,
FullName = $"{x.FirstName } {x.LastName} ",
Books = x.Books.Select(g => new BookViewModel()
{
ID = g.ID,
Name = g.Name
}).ToList()
}).ToList();
}
}
}
My Author View Model:
namespace ViewModels
{
public class AuthorViewModel
{
public AuthorViewModel()
{
Books = new List<BookViewModel>();
}
public int ID { get; set; }
public string FullName { get; set; }
public List<BookViewModel> Books { get; set; }
}
}
Books View Model:
namespace ViewModels
{
public class BookViewModel
{
public int ID { get; set; }
public string Name { get; set; }
public AuthorViewModel Author { get; set; }
}
}
Controllers:
public class HomeController : Controller
{
private readonly IAuthorService _AuthorService;
// GET: Home
public HomeController(IAuthorService authorService)
{
_AuthorService = authorService;
}
public ActionResult Index()
{
List<AuthorViewModel> Authors = _AuthorService.GetAll();
ViewBag.myAuthors = Authors;
return View();
}
[HttpGet]
public ActionResult Books(int id)
{
List<AuthorViewModel> Authors = _AuthorService.GetAll();
ViewBag.Books = from a in Authors
where a.ID == id
select a.Books;
return View();
}
}
The index contorller work fine. But when I call the Books controller it gives me this error in my browser:
'System.Collections.Generic.List' does not contain a definition for 'ID'
Source error:
Line 18: {
Line 19: <tr>
Line 20: <td>@book.ID</td> // this is the error line
Line 21: <td>@book.Name</td>
Line 22: <td><a href="~/Home/Index">Home</a></td>
Source File: C:\Users\Jack\Desktop\WebApp ver 1.2\WebApp\Views\Home\Books.cshtml Line: 20
The View Model for Books:
@{
ViewBag.Title = "Books";
}
<h2>Books</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Action</th>
</tr>
@foreach (var book in ViewBag.Books)
{
<tr>
<td>@book.ID</td>
<td>@book.ID</td>
<td><a href="~/Home/Index">Home</a></td>
</tr>
}
</table>
Here in the Index View Model everything works fine:
@{
ViewBag.Title = "Index";
}
<h2>Author List</h2>
<table>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Details</th>
<th>Books</th>
</tr>
@foreach (var author in ViewBag.myAuthors)
{
<tr>
<td>@author.ID</td>
<td>@author.FullName</td>
<td><a href="~/Home/Meh/@author.ID">Details</a></td>
<td><a href="~/Home/Books/@author.ID">Books</a></td>
</tr>
}
</table>
Any guidence and help why I can't list the books in the view would be much appreciated.
Upvotes: 1
Views: 160
Reputation: 2607
You are storing List of authors as ViewBag.Books. The query which you are using in Books
action will store list of Authors with only one column 'Books' in viewbag, so you do not have ID column in the ViewBag.Books.
you should modify the query as below.
ViewBag.Books = (from a in Authors
where a.ID == id select a).FirstOrDefault().Books;
Upvotes: 3
Reputation: 4526
Your main issue is that in your select you are picking a.Books i.e. list of books
ViewBag.Books = from a in Authors
where a.ID == id
select a.Books; //Here you select books, so you'll get a list of List<Books>
You should change your cshtml:
@foreach (var books in ViewBag.Books)
{
@foreach (var book in books ){
<tr>
<td>@book.ID</td>
<td>@book.ID</td>
<td><a href="~/Home/Index">Home</a></td>
</tr>
}
}
Or change your select.
In any case, you should not use Viewbag in this scenario, you define your Model in the view and pass it with the controller like MVC should act like.
Upvotes: 2