Books View, Controller ja Model

Controller

The SchoolContext _context tells the Controller and View what the database looks like and all the information inside it. Index lets us view all the information about Books in general using the ToListAsync() method which puts all the books into a table. Create first gives us the Create view and then when the information is given, it checks that the model is correct and if it is, adds it to the database and takes you back to the Index. It also uses SaveChangesAsync to save the information Delete deletes the given selection after checking that it exists. It first uses FirstorDefaultAsync which returns the first element of the sequence asynchronously. And uses the Lambda method to briefly show the anonymous method And then saves it to the database when it deletes the selected object.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using TallinnaRakenduslikKolledzKaur.Data;
using TallinnaRakenduslikKolledzKaur.Models;

namespace TallinnaRakenduslikKolledzKaur.Controllers
{
    public class BooksController : Controller
    {
        private readonly SchoolContext _context;

        public BooksController(SchoolContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Index()
        {
            return View(await _context.Books.ToListAsync());
        }
        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Books.Add(book);
                await _context.SaveChangesAsync();
            }

            return RedirectToAction("Index");
        }

        [HttpGet]
        public async Task<IActionResult> Delete(int? id)
        {
            ViewData["deletion"] = true;
            if (id == null)
            {
                return NotFound();
            }
            var books = await _context.Books.FirstOrDefaultAsync(b => b.BookId == id);
            if (books == null)
            {
                return NotFound();
            }
            return View(books);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(Book book)
        {
            if (await _context.Books.AnyAsync(b => b.BookId == book.BookId))
            {
                _context.Books.Remove(book);
                await _context.SaveChangesAsync();
            }
            return RedirectToAction("Index");
        }
    }
}

Mudel

using System.ComponentModel.DataAnnotations;

namespace TallinnaRakenduslikKolledzKaur.Models
{
    public class Book
    {
        [Key]
        public int BookId { get; set; }
        public string Title { get; set; }
        public int? PageCount { get; set; }
        public int TotalStock { get; set; }
        public int? AmountBorrowed { get; set; }
        public int? CurrentStock
        {
            get { return TotalStock-AmountBorrowed; }
        }


    }
}

View

Create View et teha uus raamat

@model TallinnaRakenduslikKolledzKaur.Models.Book
@{ViewData["Title"] = "Uus Kursus";}
<h1>Tee uus kursus</h1>
<h4>Sisesta info:</h4>
<hr />
<div class="row">
	<div class="col-md-4">
		<form asp-action="Create">
			<div asp-validation-summary="ModelOnly" class="text-danger"></div>
			<!--<div>
				<label asp-for="CourseId" class="control-label"></label>
				<input asp-for="CourseId" class="form-control" />
				<span asp-validation-for="CourseId" class="text-danger"></span>
			</div> -->
			<div>
				<label asp-for="Title" class="control-label"></label>
				<input asp-for="Title" class="form-control" />
				<span asp-validation-for="Title" class="text-danger"></span>
			</div>
			<div>
				<label asp-for="PageCount" class="control-label"></label>
				<input asp-for="PageCount" class="form-control" />
				<span asp-validation-for="PageCount" class="text-danger"></span>
			</div>
			<div>
				<label asp-for="TotalStock" class="control-label"></label>
				<input asp-for="TotalStock" class="form-control" />
				<span asp-validation-for="TotalStock" class="text-danger"></span>
			</div>
			<div>
				<label asp-for="AmountBorrowed" class="control-label"></label>
				<input asp-for="AmountBorrowed" class="form-control" />
				<span asp-validation-for="AmountBorrowed" class="text-danger"></span>
			</div>
			<div class="form-group">
				<input type="submit" value="Tee uus" class="btn btn-primary" /> | <a asp-action="Index" class="btn btn-outline-primary">Tühista</a>
			</div>
		</form>
	</div>
</div>

Delete view et kustutada raamat

@model TallinnaRakenduslikKolledzKaur.Models.Book

@{
	ViewData["Title"] = "Kustuta Õpetaja";
}

&lt;h1>Kustuta Õpetaja&lt;/h1>
&lt;h3>
	Kas oled kindel et tahad kustudada @Html.DisplayFor(Model => Model.Title)
&lt;/h3>
&lt;div>
	&lt;h4>Raamat&lt;/h4>
	&lt;hr />
	&lt;dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Title)&lt;/dt>
	&lt;dt class="col-sm-10">@Html.DisplayFor(Model => Model.Title)&lt;/dt>
	&lt;dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.PageCount)&lt;/dt>
	&lt;dt class="col-sm-10">@Html.DisplayFor(Model => Model.PageCount)&lt;/dt>
	&lt;dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.TotalStock)&lt;/dt>
	&lt;dt class="col-sm-10">@Html.DisplayFor(Model => Model.TotalStock)&lt;/dt>
	&lt;dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.AmountBorrowed)&lt;/dt>
	&lt;dt class="col-sm-10">@Html.DisplayFor(Model => Model.AmountBorrowed)&lt;/dt>

	&lt;form asp-action="Delete">
		&lt;input type="hidden" asp-for="BookId" />
		&lt;input type="submit" value="Kustuta Õpetaja" class="btn btn-danger" /> | &lt;a asp-action="Index">Tühista&lt;/a>
	&lt;/form>
&lt;/div>

Index view et näha kõik raamatud

@model IEnumerable<TallinnaRakenduslikKolledzKaur.Models.Book>

@{
	ViewData["Title"] = "Raamatud";
}
<h2>Raamatud</h2>
<p>
	<a asp-action="Create">Tee uus raamat</a>
</p>

<table class="table">
	<thead>
		<tr>
			<th>@Html.DisplayNameFor(Model => Model.BookId)</th>
			<th>@Html.DisplayNameFor(Model => Model.Title)</th>
			<th>@Html.DisplayNameFor(Model => Model.PageCount)</th>
			<th>@Html.DisplayNameFor(Model => Model.TotalStock)</th>
			<th>@Html.DisplayNameFor(Model => Model.AmountBorrowed)</th>
			<th>@Html.DisplayNameFor(Model => Model.CurrentStock)</th>
			<th>Tööriistad</th>
		</tr>
	</thead>

	<tbody>
		@foreach (var books in Model)
		{
			<tr>
				<td>@Html.DisplayFor(modelItem => books.BookId)</td>
				<td>@Html.DisplayFor(modelItem => books.Title)</td>
				<td>@Html.DisplayFor(modelItem => books.PageCount)</td>
				<td>@Html.DisplayFor(modelItem => books.TotalStock)</td>
				<td>@Html.DisplayFor(modelItem => books.AmountBorrowed)</td>
				<td>@Html.DisplayFor(modelItem => books.CurrentStock)</td>
				<td>
					<a asp-action="Delete" asp-route-id="@books.BookId">Kustuta</a>
					<a asp-action="Details" asp-route-id="@books.BookId">Details</a>
					<a asp-action="Edit" asp-route-id="@books.BookId">Edit</a>
				</td>
			</tr>
		}
	</tbody>
</table>