In this part we will be finishing up the functionality to assign a book to a customer and returning the book.
Source Code: https://github.com/alexcalingasan/LibraryManagement
Now let’s update our View for Create and Edit page of BorrowHistory.
Copy and paste this for your BorrowHistory create view.
@model LibraryManagement.Models.Book @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Book</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.SerialNumber, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.SerialNumber, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SerialNumber, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Publisher, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Publisher, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Publisher, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Then change your BorrowHistories Edit view code to this.
@model LibraryManagement.Models.Book @{ ViewBag.Title = "Edit"; } <h2>Edit</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Book</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.BookId) <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.SerialNumber, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.SerialNumber, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SerialNumber, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Publisher, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Publisher, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Publisher, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Then change you BorrowHistories Controller to this.
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using LibraryManagement.Models; namespace LibraryManagement.Controllers { public class BorrowHistoriesController : Controller { private ApplicationDbContext db = new ApplicationDbContext(); // GET: BorrowHistories/Create public ActionResult Create(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } var borrowHistory = new BorrowHistory { BookId = book.BookId, BorrowDate = DateTime.Now, Book = book }; ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name"); return View(borrowHistory); } // POST: BorrowHistories/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "BorrowHistoryId,BookId,CustomerId,BorrowDate,ReturnDate")] BorrowHistory borrowHistory) { if (ModelState.IsValid) { db.BorrowHistories.Add(borrowHistory); db.SaveChanges(); return RedirectToAction("Index", "Books"); } ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name", borrowHistory.CustomerId); borrowHistory.Book = db.Books.Find(borrowHistory.BookId); return View(borrowHistory); } // GET: BorrowHistories/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } BorrowHistory borrowHistory = db.BorrowHistories .Include(b => b.Book) .Include(c => c.Customer) .Where(b => b.BookId == id && b.ReturnDate == null) .FirstOrDefault(); if (borrowHistory == null) { return HttpNotFound(); } return View(borrowHistory); } // POST: BorrowHistories/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "BorrowHistoryId,BookId,CustomerId,BorrowDate,ReturnDate")] BorrowHistory borrowHistory) { if (ModelState.IsValid) { var borrowHistoryItem = db.BorrowHistories.Include(i => i.Book) .FirstOrDefault(i => i.BorrowHistoryId == borrowHistory.BorrowHistoryId); if (borrowHistoryItem == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } borrowHistoryItem.ReturnDate = DateTime.Now; db.SaveChanges(); return RedirectToAction("Index", "Books"); } return View(borrowHistory); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }
Now go and run your project and you should have now functionality to borrow and return a book.
Related Articles:
- Library Management using ASP.Net MVC (Part 1) Books CRUD Function
- Library Management using ASP.Net MVC (Part 2) Request Lifecycle
- Library Management using ASP.Net MVC (Part 3) Adding the Customers
- Library Management using ASP.Net MVC (Part 4) Entity Framework Relationship and Navigation Properties
- Library Management using ASP.Net MVC (Part 5) Eager Loading and Projection