示例#1
0
        // GET: Admin/Books/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var book = await _context.Books.FindAsync(id);

            if (book == null)
            {
                return(NotFound());
            }

            BookImagePath bookModel = new BookImagePath {
                Id          = book.Id,
                ISBN        = book.ISBN,
                Name        = book.Name,
                Description = book.Description,
                Genre       = book.Genre,
                Author      = book.Author,
                PublishDate = book.PublishDate,
                Amount      = book.Amount,
                Available   = book.Available
            };

            return(View(bookModel));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("Id,ISBN,Name,Description,Genre,Author,PublishDate,Image,Amount,Available")] BookImagePath model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                if (model.Image != null)
                {
                    string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "BookImages");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    model.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                }
                var book = new Book {
                    Name        = model.Name,
                    ISBN        = model.ISBN,
                    Description = model.Description,
                    Genre       = model.Genre,
                    Author      = model.Author,
                    PublishDate = model.PublishDate,
                    Image       = uniqueFileName,
                    Amount      = model.Amount,
                    Available   = model.Available
                };

                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
示例#3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,ISBN,Name,Description,Genre,Author,PublishDate,Image,Amount,Available")] BookImagePath book)
        {
            if (id != book.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string uniqueFileName = null;
                    if (book.Image != null)
                    {
                        string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "BookImages");
                        uniqueFileName = Guid.NewGuid().ToString() + "_" + book.Image.FileName;
                        string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                        book.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                    }

                    Book insertBook = new Book {
                        Id          = book.Id,
                        ISBN        = book.ISBN,
                        Name        = book.Name,
                        Description = book.Description,
                        Genre       = book.Genre,
                        Author      = book.Author,
                        PublishDate = book.PublishDate,
                        Image       = uniqueFileName,
                        Amount      = book.Amount,
                        Available   = book.Available
                    };

                    _context.Update(insertBook);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookExists(book.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }