Library Management using ASP.Net MVC (Part 6) Borrowing and returning a book

In this part we will be finishing up the functionality to assign a book to a customer and returning the book.

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.BorrowHistory

@{
    ViewBag.Title = "Borrow a book";
}
<h2>Assign a Book</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<div class="form-horizontal">

<hr />

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.BookId)
        @Html.HiddenFor(model => model.BorrowDate)
        @Html.HiddenFor(model => model.ReturnDate)
<div class="form-group">
            @Html.LabelFor(model => model.CustomerId, "Customer", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
                @Html.DropDownList("CustomerId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CustomerId, "", new { @class = "text-danger" })</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Assign" class="btn btn-default" /></div>
</div>
</div>
}
<div>
    @Html.ActionLink("Back to Book List", "Index", "Books")</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Then this code for your BorrowHistory Edit view.

@model LibraryManagement.Models.BorrowHistory

@{
    ViewBag.Title = "Return a book";
}
<h2>Return a Book</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<div class="form-horizontal">

<hr />

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.BorrowHistoryId)
    @Html.HiddenFor(model => model.BookId)
    @Html.HiddenFor(model => model.BorrowDate)
    @Html.HiddenFor(model => model.ReturnDate)
    @Html.HiddenFor(model => model.CustomerId)
<div class="form-group">
<div class="col-md-10">

@string.Format("You are about to return {0} which was borrowed by {1}.", Model.Book.Title, Model.Customer.Name)</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Return" class="btn btn-default" /></div>
</div>
</div>
}
<div>
    @Html.ActionLink("Back to Book List", "Index", "Books")</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

 

Then change you BorrowHistories Controller to this.

<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>
    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 };
            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);
            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.Find(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 a functionality to borrow and return a book.

assign book

return a book

Related Articles:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s