MVC ASP 3表连接然后显示结果

时间:2022-09-05 15:42:12

For a project, I'm displaying books for adding to basket. I need to join 3 tables in order to show the formats available: I do: Books to BookFormats on bookID, then BookFormats to Format on formatID. This is a prelaid out database which i cannot change. I wrote a LINQ query to display them all and made a view model with the properties of all 3.

对于一个项目,我正在展示用于添加到篮子的书籍。我需要加入3个表来显示可用的格式:我这样做:BookID到bookID上的BookFormats,然后在formatID上将BookFormats格式化为格式。这是一个预先输出的数据库,我无法改变。我写了一个LINQ查询来显示它们并创建了一个具有所有3属性的视图模型。

The linq query:

linq查询:

        IEnumerable<BookAndFormatViewModel> IEBooksRepository.GetAllBooksWithFormats()
    {
        var query =
            from book in eBooks.Books
            join format in eBooks.BookFormats on book.BookID equals format.BookID
            join formatName in eBooks.Formats on format.FormatID equals formatName.FormatID
            select new BookAndFormatViewModel();
        return query.ToList();

The view model for the three:

三者的视图模型:

    public int BookID { get; set; }
    public string ReferenceNumber { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public int FormatID { get; set; }
    public string FormatType { get; set; }

The controller code:

控制器代码:

        IEnumerable<BookModel> books = repository.GetAllBooks();
        IEnumerable<BookAndFormatViewModel> booksWithFormat = repository.GetAllBooksWithFormats();

        if (books.Count() == 0)
        {
            return PartialView("NoBooks");

        }
        return PartialView("Books", booksWithFormat);   

When I run it it displays empty data just 0's and null fields.

当我运行它时,它只显示0和空字段的空数据。

1 个解决方案

#1


0  

The fix in the end was to change the constructor of the model and then create the model like so:

最后的修复是更改模型的构造函数,然后像这样创建模型:

            var query =
            from book in eBooks.Books
            join format in eBooks.BookFormats on book.BookID equals format.BookID
            join formatName in eBooks.Formats on format.FormatID equals formatName.FormatID
            select new BookAndFormatViewModel
            {
                BookID = book.BookID,
                ReferenceNumber = book.ReferenceNumber,
                Title = book.Title,
                Price = book.Price,
                FormatID = format.FormatID,
                FormatType = formatName.FormatType
            };

#1


0  

The fix in the end was to change the constructor of the model and then create the model like so:

最后的修复是更改模型的构造函数,然后像这样创建模型:

            var query =
            from book in eBooks.Books
            join format in eBooks.BookFormats on book.BookID equals format.BookID
            join formatName in eBooks.Formats on format.FormatID equals formatName.FormatID
            select new BookAndFormatViewModel
            {
                BookID = book.BookID,
                ReferenceNumber = book.ReferenceNumber,
                Title = book.Title,
                Price = book.Price,
                FormatID = format.FormatID,
                FormatType = formatName.FormatType
            };