public ActionResult Index(int bookId, string category, string searchString, int resultsPerPage = 10, int page = 1) { var book = db.Books.Single(b => b.ID == bookId); BookCard bc; bool conflict = false; //no one is reading it? borrow! if (book.Reader == null) { bc = new BookCard { BookID = bookId, BorrowDate = DateTime.Now, Reader = User.Identity.Name }; book.Reader = User.Identity.Name; db.Cards.Add(bc); db.Entry(book).State = EntityState.Modified; } //user is the reader? return! else if (book.Reader == User.Identity.Name) { bc = db.Cards.Single(c => c.BookID == bookId && c.ReturnDate == null); bc.ReturnDate = DateTime.Now; book.Reader = null; db.Entry(bc).State = EntityState.Modified; db.Entry(book).State = EntityState.Modified; } else //this will probably only happen if the book was borrowed by someone else //after the page was loaded but before the button click { conflict = true; } db.SaveChanges(); if (Request.IsAjaxRequest()) { return(Content((HtmlHelpers.GetButton(bookId, book.Reader, User.Identity.Name, conflict)).ToHtmlString())); } var books = db.Books.Where(b => b.Status == true).ToList(); var model = BuildIndexViewModel(category, searchString, resultsPerPage, page, books); return(View(model)); }