// GET: BookTables/Loan/5 public ActionResult Loan(int?id, string Name) { //get member's names for drop down list var MemberList = new List <string>(); var MemberQuery = from item in db.MemberTable select item; //populate view model to allow data from different tables to be used in same view var model = new MemberBookVM { MemberTable = MemberQuery.Select(a => new SelectListItem { Text = a.Name, Value = a.Name }) }; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookTable bookTable = db.BookTable.Find(id); if (bookTable == null) { return(HttpNotFound()); } return(View(model)); }
public ActionResult Loan(int id, MemberBookVM model) { //get member's names for drop down list var MemberList = new List <string>(); var MemberQuery = from item in db.MemberTable select item; //populate view model var model1 = new MemberBookVM { MemberTable = MemberQuery.Select(a => new SelectListItem { Text = a.Name, Value = a.MemberID.ToString() }) }; if (ModelState.IsValid) { //used to get the instance of the member where the name is the same as the one saved to the vm var result = db.MemberTable.FirstOrDefault(s => s.Name == model.memberName); //grabs the book from the database that is currently being looked at var book = db.BookTable.FirstOrDefault(s => s.BookID == id); if (book != null) { //changes the onloan status to 1 which makes it 'on loan' book.OnLoan = 1; //puts the member id of the selected member into the memberid section in the book table book.MemberID = result.MemberID; db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } //ViewBag.MemberID = new SelectList(db.MemberTable, "MemberID", "Name", bookTable.MemberID); return(View("Index")); }