无法将表从SQL列出到MVC视图 [英] Can't list table from SQL to MVC View
问题描述
这是我的存储库:
namespace Repositories
{
public class AuthorRepository : IAuthorRepository
{
AppContext myDB = new AppContext();
public List<Author> GetAllFromRepo()
{
return myDB.Authors.ToList();
}
}
}
我的服务文件:
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();
}
}
}
我的作者视图模型:
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:
Books View Model:
namespace ViewModels
{
public class BookViewModel
{
public int ID { get; set; }
public string Name { get; set; }
public AuthorViewModel Author { get; set; }
}
}
控制器:
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();
}
}
索引控制器工作正常.但是当我调用Books控制器时,它在浏览器中给了我这个错误:
The index contorller work fine. But when I call the Books controller it gives me this error in my browser:
'System.Collections.Generic.List'不包含'ID'的定义
'System.Collections.Generic.List' does not contain a definition for 'ID'
源错误:
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>
源文件:C:\ Users \ Jack \ Desktop \ WebApp版本1.2 \ WebApp \ Views \ Home \ Books.cshtml行:20
Source File: C:\Users\Jack\Desktop\WebApp ver 1.2\WebApp\Views\Home\Books.cshtml Line: 20
书籍的视图模型:
@{
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.
推荐答案
您正在将作者列表存储为ViewBag.Books.您在 Books
操作中使用的查询将在列表中存储只有一列"Books"的Authors列表,因此ViewBag.Books中没有ID列.您应该按如下所示修改查询.
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;
这篇关于无法将表从SQL列出到MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!