Пример #1
0
        public ActionResult Create(BookAuthorViewModels model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    string filename = uploadfile(model.file) ?? string.Empty;

                    if (model.AuthorId == -1)
                    {
                        ViewBag.message = "Please Select Author From List";

                        return(View(getallauthors()));
                    }
                    var  author = authorRepos.find(model.AuthorId);
                    Book book   = new Book
                    {
                        Id          = model.BookId,
                        Title       = model.Title,
                        Description = model.Description,
                        Author      = author,
                        imageurl    = filename
                    };
                    bookRespo.add(book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }
            ModelState.AddModelError("", "..you have to fill all required fields..");
            return(View(getallauthors()));
        }
Пример #2
0
        public ActionResult Edit(int id, BookAuthorViewModels viewmodel)
        {
            try
            {
                // TODO: Add update logic here
                string filename = uploadfile(viewmodel.file, viewmodel.imageurl);

                var  author = authorRepos.find(viewmodel.AuthorId);
                Book book   = new Book
                {
                    Id          = viewmodel.BookId,
                    Title       = viewmodel.Title,
                    Description = viewmodel.Description,
                    Author      = author,
                    imageurl    = filename
                };
                bookRespo.update(viewmodel.BookId, book);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Пример #3
0
        // GET: Book/Create
        public ActionResult Create()
        {
            var model = new BookAuthorViewModels
            {
                Authors = fillselectlist()
            };

            return(View(model));
        }
Пример #4
0
        public BookAuthorViewModels getallauthors()
        {
            var vmodel = new BookAuthorViewModels
            {
                Authors = fillselectlist()
            };

            return(vmodel);
        }
Пример #5
0
        // GET: Book/Edit/5
        public ActionResult Edit(int id)
        {
            var book      = bookRespo.find(id);
            var authorid  = book.Author == null ? book.Author.Id = 0 :  book.Author.Id;
            var viewmodel = new BookAuthorViewModels
            {
                BookId      = book.Id,
                Title       = book.Title,
                Description = book.Description,
                AuthorId    = authorid,
                Authors     = authorRepos.list().ToList(),
                imageurl    = book.imageurl
            };

            return(View(viewmodel));
        }