Пример #1
0
        public async Task <ActionResult> Create(CategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (BooksAdded != null)
                {
                    model.Books = BooksAdded;
                }
                else
                {
                    model.Books = new List <BookViewModel>();
                }
                using (DbContextTransaction dbTran = db.Database.BeginTransaction())
                {
                    try
                    {
                        Category category = new Category();
                        category.CategoryName = model.Name;
                        db.Categories.Add(category);

                        if (BooksAdded != null)
                        {
                            foreach (var book in BooksAdded)
                            {
                                Book b = await db.Books.FindAsync(book.Id);

                                category.Books.Add(b);
                            }
                        }
                        await db.SaveChangesAsync();

                        dbTran.Commit();
                        BooksAdded = null;
                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateException dbEx)
                    {
                        SqlException sqlEx = dbEx.GetBaseException() as SqlException;
                        if (sqlEx.Errors[0].Number == 2601)
                        {
                            TempData["ErrorMessage"] = "Duplicated code";
                        }
                        else
                        {
                            TempData["ErrorMessage"] = sqlEx.Errors[0].Message;
                        }

                        dbTran.Rollback();

                        List <BookViewModel> pendingBooks = await GetPendingBooks();

                        ViewBag.BookId = new SelectList(pendingBooks, "Id", "Title");

                        return(View(model));
                    }
                }
            }

            return(View(model));
        }
Пример #2
0
        public async Task <ActionResult> Create([Bind(Include = "AuthorId,AuthorName,Nacionality")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Пример #3
0
        public async Task <ActionResult> Edit(BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Book book = await db.Books.FindAsync(model.Id);

                    book.Title           = model.Title;
                    book.AuthorId        = model.AuthorId;
                    book.Price           = model.Price;
                    book.Editorial       = model.Editorial;
                    db.Entry(book).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException dbEx)
                {
                    SqlException sqlEx = dbEx.GetBaseException() as SqlException;
                    if (sqlEx.Errors[0].Number == 2601)
                    {
                        TempData["ErrorMessage"] = "Duplicated code";
                    }
                    else
                    {
                        TempData["ErrorMessage"] = sqlEx.Errors[0].Message;
                    }

                    ViewBag.AuthorId = new SelectList(db.Authors, "AuthorId", "AuthorName", model.AuthorId);

                    return(View(model));
                }
            }
            ViewBag.AuthorId = new SelectList(db.Authors, "AuthorId", "AuthorName", model.AuthorId);
            return(View(model));
        }