Exemple #1
0
        public ActionResult Create(Book book)
        {
            try
            {
                if (book.Type == null)
                {
                    book.Type = book.TypeHidden;
                }

                if ((book.Repeat == 1) && (book.RepeatPeriod == Book.BookRepeatPeriod.NoRepeat))
                {
                    ModelState.AddModelError("", "Repeat Period is necessary");
                }


                if (ModelState.IsValid == false)
                {
                    User user = (User)Session["User"];

                    var listCategories = _categoryRepository.GetByUser(user.IdUser);
                    var listAccounts   = _accountRepository.GetByUser(user.IdUser);

                    ViewBag.listCategories = new SelectList(listCategories, "IdCategory", "Description", "Selecione uma categoria");
                    ViewBag.listAccounts   = new SelectList(listAccounts, "IdAccount", "Description", "Selecione uma conta");


                    return(View(book));
                }


                _bookRepository.Add(book);
                _bookRepository.SaveChanges();

                if (book.NParcels > 0)
                {
                    Parcel parcel = new Parcel();
                    parcel.IdBook = book.IdBook;

                    for (int i = 1; i <= book.NParcels; i++)
                    {
                        parcel.ParcelNumber = i;
                        _parcelRepository.Add(parcel);
                        _parcelRepository.SaveChanges();
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Não foi possível salvar as mudanças. Tente Novamente.");
                return(View());
            }
        }