Example #1
0
        public ActionResult Save(Lend lend)
        {
            if (!ModelState.IsValid)
            {
                var lendFormViewModel = new LendFormViewModel
                {
                    Borrower = new Borrower(),
                    Books    = _db.Books.ToList()
                };
                return(View("LendForm", lendFormViewModel));
            }

            if (lend.Id == 0)
            {
                lend.DateLent = DateTime.Today;
                _db.Lends.Add(lend);
            }
            else //lend.Id != 0
            {
                var lendInDb = _db.Lends.Include(l => l.LendDetails).Single(l => l.Id == lend.Id);
                lendInDb.BorrowerId  = lend.BorrowerId;
                lendInDb.LendDetails = lend.LendDetails;
            }
            _db.SaveChanges();
            return(RedirectToAction("Index", "Lends"));
        }
Example #2
0
        public ActionResult New(int id)
        {
            var lendFormViewModel = new LendFormViewModel
            {
                Borrower = _db.Borrowers.Single(b => b.Id == id),
                Books    = _db.Books.ToList()
            };

            return(View("LendForm", lendFormViewModel));
        }