public ActionResult Edit(int id, Author author)
        {
            try
            {
                authorRepository.Update(id, author);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
示例#2
0
        public ActionResult Edit(BookAuthorViewModel viewModel)
        {
            try
            {
                string fileName = UploadFile(viewModel.File) ?? string.Empty;
                if (viewModel.File != null)
                {
                    string uploads = Path.Combine(hosting.WebRootPath, "uploads");
                    fileName = viewModel.File.FileName;
                    string fullPath = Path.Combine(uploads, fileName);

                    //Delete the old file
                    string oldFileName         = viewModel.ImageUrl;
                    string fullPathOldFileName = Path.Combine(uploads, oldFileName);

                    if (fullPath != fullPathOldFileName)
                    {
                        System.IO.File.Delete(fullPathOldFileName);
                        //save the new file
                        viewModel.File.CopyTo(new FileStream(fullPath, FileMode.Create));
                    }
                }

                var  author = authorRepository.Find(viewModel.AuthorId);
                Book book   = new Book
                {
                    Id          = viewModel.BookId,
                    Title       = viewModel.Title,
                    Description = viewModel.Description,
                    Author      = author,
                    ImageUrl    = fileName
                };
                bookStoreRepoitory.Update(viewModel.BookId, book);
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }