public ActionResult Create(BookAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var fileName = UploadFile(model.File) ?? string.Empty;

                    if (model.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author from the list";

                        return(View(GetAllAuthors()));
                    }
                    var author = _authorRepository.Find(model.AuthorId);
                    var book   = new Book
                    {
                        Id          = model.BookId,
                        Title       = model.Title,
                        Description = model.Description,
                        Author      = author,
                        ImageUrl    = fileName
                    };
                    _bookRepository.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception exp)
                {
                    return(View(exp.Message));
                }
            }

            ModelState.AddModelError("", "You have to fill all the required fields correctely");
            return(View(GetAllAuthors()));
        }
        public IActionResult Create(BookAuthorViewModel bookModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(bookModel.File) ?? string.Empty;

                    if (bookModel.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author from the list";
                        return(View(GetAllAuthorsModel()));
                    }
                    var  author = authorRepository.Find(bookModel.AuthorId);
                    Book book   = new Book
                    {
                        Id          = bookModel.BookId,
                        Title       = bookModel.Title,
                        Description = bookModel.Description,
                        ImageUrl    = fileName,
                        Author      = author
                    };
                    bookRepository.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }catch
                {
                    return(View());
                }
            }
            ModelState.AddModelError("", "You have to fill all required fields!");
            return(View(GetAllAuthorsModel()));
        }
Пример #3
0
 public ActionResult Create(Author author)
 {
     try
     {
         AuthorRepo.Add(author);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
 public ActionResult Create(Author author)
 {
     try
     {
         authorRepository.Add(author);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         ViewBag.Message = "Error Ocured";
         return(View());
     }
 }
 public IActionResult Create(Author author)
 {
     if (ModelState.IsValid)
     {
         try
         {
             authorRepository.Add(author);
             return(RedirectToAction(nameof(Index)));
         }
         catch
         {
             return(View(author));
         }
     }
     ModelState.AddModelError("", "You have to fill all required fields!");
     return(View());
 }
        public ActionResult Create(BookAuthorViewModel model)
        {
            var author = authorRepository.Find(model.AuthorId);


            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = string.Empty;
                    if (model.File != null)
                    {
                        string Uploads = Path.Combine(hosting.WebRootPath, "Uploads");
                        fileName = model.File.FileName;
                        string FullPath = Path.Combine(Uploads, fileName);
                        model.File.CopyTo(new FileStream(FullPath, FileMode.Create));
                    }

                    if (model.AuthorId == 0)
                    {
                        ViewBag.Message = "Please Select Author !";

                        return(View(getAuthors()));
                    }

                    Book book = new Book
                    {
                        Id          = model.BookId,
                        Title       = model.Title,
                        Description = model.Description,
                        Author      = author,
                        ImageURL    = fileName
                    };
                    bookRepository.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }

            ModelState.AddModelError(" ", "Please Fill Required Fields !");
            return(View(getAuthors()));
        }
Пример #7
0
        public ActionResult Create(Author author)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    authorRepository.Add(author);

                    return(RedirectToAction("Index", "Author"));
                }
                catch (Exception)
                {
                    return(View());
                }
            }

            ModelState.AddModelError("", "Please Fill All Required Fields !");
            return(View());
        }
Пример #8
0
        public ActionResult Create(BookAuthorViewModel model)
        {
            if (Validate(model.AuthorId) == false)
            {
                return(Create());
            }

            try
            {
                var author = authorRepository.Find(model.AuthorId);
                model.ImageUrl = model.File.FileName;
                var book = GetNewBook(model, author);
                bookRepository.Add(book);
                UploadFile(model.File, book.Id);
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(Create());
            }
        }
Пример #9
0
        public ActionResult Create(BookAuthorViewModel vModel)
        {
            // if all validations are true(achieved)
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(vModel.File) ?? string.Empty;

                    if (vModel.AuthorId < 0)
                    {
                        ViewBag.Message = "Please select an Author from the list";

                        return(View(GetAllAuthors()));
                    }
                    var author = authorRepository.Find(vModel.AuthorId);
                    var book   = new Book
                    {
                        Id          = vModel.BookId,
                        Title       = vModel.Title,
                        Description = vModel.Description,
                        ImageUrl    = fileName,
                        Author      = author
                    };
                    bookRepository.Add(book);

                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }
            //ModelState will passed to validation-summary
            ModelState.AddModelError("", "You have to fill all the required fields");
            return(View(GetAllAuthors()));
        }