Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e78dba3

Browse files
dynamic view component in asp.net core
1 parent 1fa526a commit e78dba3

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Components/TopBooksViewComponent.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using Webgentle.BookStore.Repository;
67

78
namespace Webgentle.BookStore.Components
89
{
910
public class TopBooksViewComponent : ViewComponent
1011
{
11-
public async Task<IViewComponentResult> InvokeAsync()
12+
private readonly BookRepository _bookRepository;
13+
14+
public TopBooksViewComponent(BookRepository bookRepository)
15+
{
16+
_bookRepository = bookRepository;
17+
}
18+
19+
public async Task<IViewComponentResult> InvokeAsync(int count)
1220
{
13-
return View();
21+
var books = await _bookRepository.GetTopBooksAsync(count);
22+
return View(books);
1423
}
1524
}
1625
}

‎Webgentle.BookStore/Webgentle.BookStore/Repository/BookRepository.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ public async Task<List<BookModel>> GetAllBooks()
6767
}).ToListAsync();
6868
}
6969

70+
public async Task<List<BookModel>> GetTopBooksAsync(int count)
71+
{
72+
return await _context.Books
73+
.Select(book => new BookModel()
74+
{
75+
Author = book.Author,
76+
Category = book.Category,
77+
Description = book.Description,
78+
Id = book.Id,
79+
LanguageId = book.LanguageId,
80+
Language = book.Language.Name,
81+
Title = book.Title,
82+
TotalPages = book.TotalPages,
83+
CoverImageUrl = book.CoverImageUrl
84+
}).Take(count).ToListAsync();
85+
}
86+
7087
public async Task<BookModel> GetBookById(int id)
7188
{
7289
return await _context.Books.Where(x => x.Id == id)

‎Webgentle.BookStore/Webgentle.BookStore/Views/Home/Index.cshtml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,10 @@
1616
<div class="py-5 bg-light">
1717
<div class="container">
1818
<h3 class="h3">Top books</h3>
19+
@await Component.InvokeAsync("TopBooks", new { count = 3})
1920

20-
@*@await Component.InvokeAsync("TopBooks")*@
21-
22-
<vc:top-books></vc:top-books>
23-
24-
<div class="row">
25-
<div class="col-md-4">
26-
@*<partial name="_bookThumbnail" model="new BookModel()" />*@
27-
@Html.Partial("_bookThumbnail", new BookModel())
28-
</div>
29-
<div class="col-md-4">
30-
@*<partial name="_bookThumbnail" model="new BookModel()" />*@
31-
@await Html.PartialAsync("_bookThumbnail", new BookModel())
32-
</div>
33-
<div class="col-md-4">
34-
@*<partial name="_bookThumbnail" model="new BookModel()" />*@
35-
@{ Html.RenderPartial("_bookThumbnail", new BookModel()); }
36-
37-
</div>
38-
<div class="col-md-4">
39-
@*<partial name="_bookThumbnail" model="new BookModel()" />*@
40-
@{ await Html.RenderPartialAsync("_bookThumbnail", new BookModel()); }
41-
42-
</div>
43-
</div>
21+
@*<vc:top-books count="6" ></vc:top-books>*@
22+
4423
</div>
4524
</div>
4625

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
<h1>Hello from view components</h1>
1+
@model IEnumerable<BookModel>
2+
3+
<div class="row">
4+
@foreach (var book in Model)
5+
{
6+
<div class="col-md-4">
7+
<partial name="_bookThumbnail" model="book" />
8+
</div>
9+
}
10+
11+
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /