Esempio n. 1
0
        public ActionResult Create(BookAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                string fileName = UploadFile(model.File) == null ? string.Empty : UploadFile(model.File);

                try
                {
                    if (model.AuthorId == -1)
                    {
                        // viewbag => Dynamic Properties send data between controller and view
                        // .Message we put the property we need for example we can replace Message by any other name
                        ViewBag.Message = "Please select an author from the list ! ";

                        return(View(GetAllAuthors()));
                    }

                    var author = authorRepository.Find(model.AuthorId);


                    Book book = new Book();

                    book.Id          = model.BookId;
                    book.Title       = model.Title;
                    book.Description = model.Description;
                    book.Author      = author;
                    book.ImageUrl    = fileName;


                    bookRepository.Add(book);

                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }

            ModelState.AddModelError("", "You have to fill all required fields");
            return(View(GetAllAuthors()));
        }
        // GET: HomeController/Details/5
        public ActionResult Details(int id)
        {
            var authors = authorRepository.Find(id);

            return(View(authors));
        }
Esempio n. 3
0
        // GET: BookController/Details/5
        public ActionResult Details(int id)
        {
            var book = bookRepository.Find(id);

            return(View(book));
        }