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 9328ba0

Browse files
Inject services into views
1 parent 9444223 commit 9328ba0

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/BookController.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public async Task<ViewResult> AddNewBook(bool isSuccess = false, int bookId = 0)
5252
{
5353
var model = new BookModel();
5454

55-
ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), "Id", "Name");
56-
5755
ViewBag.IsSuccess = isSuccess;
5856
ViewBag.BookId = bookId;
5957
return View(model);
@@ -100,9 +98,6 @@ public async Task<IActionResult> AddNewBook(BookModel bookModel)
10098
}
10199
}
102100

103-
ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), "Id", "Name");
104-
105-
106101
return View();
107102
}
108103

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,10 @@ public List<BookModel> SearchBook(string title, string authorName)
112112
{
113113
return null;
114114
}
115+
116+
public string GetAppName()
117+
{
118+
return "Book store application";
119+
}
115120
}
116121
}

‎Webgentle.BookStore/Webgentle.BookStore/Repository/IBookRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public interface IBookRepository
1111
Task<BookModel> GetBookById(int id);
1212
Task<List<BookModel>> GetTopBooksAsync(int count);
1313
List<BookModel> SearchBook(string title, string authorName);
14+
15+
string GetAppName();
1416
}
1517
}

‎Webgentle.BookStore/Webgentle.BookStore/Views/Book/AddNewBook.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@using Webgentle.BookStore.Enums
2+
@inject Webgentle.BookStore.Repository.ILanguageRepository _languageRepo
23
@model BookModel
34
@{
45
ViewData["Title"] = "Add new book";
@@ -22,7 +23,7 @@
2223

2324
<div class="form-group">
2425
<label asp-for="LanguageId" class="control-label"></label>
25-
<select asp-for="LanguageId" class="form-control" asp-items="ViewBag.Language">
26+
<select asp-for="LanguageId" class="form-control" asp-items="(await _languageRepo.GetLanguages()).Select(x=> new SelectListItem() { Text = x.Name, Value = x.Id.ToString()})">
2627
<option value="">Please choose your language</option>
2728
</select>
2829
<span asp-validation-for="LanguageId" class="text-danger"></span>

‎Webgentle.BookStore/Webgentle.BookStore/Views/Shared/_header.cshtml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<header>
1+
@inject Webgentle.BookStore.Repository.IBookRepository _bookRepo
2+
3+
<header>
24
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
35
<div class="container">
46
<a class="navbar-brand" href="/">
57
<img src="~/images/logo.png" width="30" height="30" asp-append-version="true" />
6-
Book store
8+
@_bookRepo.GetAppName()
79
</a>
810
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
911
aria-expanded="false" aria-label="Toggle navigation">

0 commit comments

Comments
(0)

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