예제 #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.

        /*public async Task<IActionResult> OnPostAsync()
         * {
         *  if (!ModelState.IsValid)
         *  {
         *      return Page();
         *  }
         *
         *  _context.Attach(Book).State = EntityState.Modified;
         *
         *  try
         *  {
         *      await _context.SaveChangesAsync();
         *  }
         *  catch (DbUpdateConcurrencyException)
         *  {
         *      if (!BookExists(Book.ID))
         *      {
         *          return NotFound();
         *      }
         *      else
         *      {
         *          throw;
         *      }
         *  }
         *
         *  return RedirectToPage("./Index");
         * }*/

        public async Task <IActionResult> OnPostAsync(int?id, string[]
                                                      selectedCategories)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var bookToUpdate = await _context.Book
                               .Include(i => i.Publisher)
                               .Include(i => i.BookCategories)
                               .ThenInclude(i => i.Category)
                               .FirstOrDefaultAsync(s => s.ID == id);

            if (bookToUpdate == null)
            {
                return(NotFound());
            }
            if (await TryUpdateModelAsync <Book>(
                    bookToUpdate,
                    "Book",
                    i => i.Title, i => i.Author,
                    i => i.Price, i => i.PublishingDate, i => i.Publisher))
            {
                UpdateBookCategories(_context, selectedCategories, bookToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            //Apelam UpdateBookCategories pentru a aplica informatiile din checkboxuri la entitatea Books care
            //este editata
            UpdateBookCategories(_context, selectedCategories, bookToUpdate);
            PopulateAssignedCategoryData(_context, bookToUpdate);
            return(Page());
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.

        /* public async Task<IActionResult> OnPostAsync()
         * {
         *   if (!ModelState.IsValid)
         *   {
         *       return Page();
         *
         *   }
         *
         *   _context.Book.Add(Book);
         *   await _context.SaveChangesAsync();
         *
         *
         *   return RedirectToPage("./Index");
         * }*/
        public async Task <IActionResult> OnPostAsync(string[] selectedCategories)
        {
            var newBook = new Book();

            if (selectedCategories != null)
            {
                newBook.BookCategories = new List <BookCategory>();
                foreach (var cat in selectedCategories)
                {
                    var catToAdd = new BookCategory
                    {
                        CategoryID = int.Parse(cat)
                    };
                    newBook.BookCategories.Add(catToAdd);
                }
            }
            if (await TryUpdateModelAsync <Book>(
                    newBook,
                    "Book",
                    i => i.Title, i => i.Author,
                    i => i.Price, i => i.PublishingDate, i => i.PublisherID))
            {
                _context.Book.Add(newBook);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            PopulateAssignedCategoryData(_context, newBook);
            return(Page());
        }
예제 #3
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Publisher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PublisherExists(Publisher.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #4
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Publisher = await _context.Publisher.FindAsync(id);

            if (Publisher != null)
            {
                _context.Publisher.Remove(Publisher);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Category = await _context.Category.FindAsync(id);

            if (Category != null)
            {
                _context.Category.Remove(Category);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #7
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Book = await _context.Book.FindAsync(id);

            if (Book != null)
            {
                _context.Book.Remove(Book);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }