Exemple #1
0
        public ActionResult Create([Bind(Include = "Id,OrderDate,ReceptionDate,ReturnDate,UserId,BookId")] Order order, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var DDLValue     = Int32.Parse(Request.Form["BookId"]);
                    var availability = _bookRepo.Availability(DDLValue);
                    if (availability == "No")
                    {
                        throw new Exception();
                    }
                    else
                    {
                        order.UserId    = User.Identity.GetUserId();
                        order.OrderDate = System.DateTime.Now.Date;
                        DateTime date = order.OrderDate;
                        order.ReceptionDate = date.AddDays(2);
                        order.ReturnDate    = date.AddDays(30);


                        _repo.AddOrder(order);
                        _repo.SaveChanges();
                        _bookRepo.ChangeAvailability(order.BookId, "No");
                        _bookRepo.SaveChanges();
                    }
                }
                catch
                {
                    ViewBag.Error = true;
                    return(RedirectToAction("Create", new { error = true }));
                }
                //ViewBag.Error = false;


                ViewBag.BookId = new SelectList(_context.Book, "Id", "Title", order.BookId);
            }
            return(RedirectToAction("MyOrder"));
        }