Beispiel #1
0
        public async Task <IActionResult> Edit(int id, [Bind("id,name")] Genre genre)
        {
            if (id != genre.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(genre);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GenreExists(genre.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, [Bind("id,name,genreId,authors")] Book book)
        {
            if (id != book.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(book);

                    this._context.Entry(book).Collection(book => book.authorBookList).Load( );
                    book.authorBookList.Clear( );
                    foreach (int author in book.authors)
                    {
                        book.authorBookList.Add(
                            new AuthorBook( )
                        {
                            authorId = author
                        }
                            );
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookExists(book.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["genreId"] = new SelectList(_context.genres, "id", "name", book.genreId);
            return(View(book));
        }
        public async Task <IActionResult> Edit(int id, [Bind("id,firstName,lastName,country")] Author author)
        {
            if (id != author.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                //proveravam da li je menjano ime ili country
                bool isOld = this._context.authors.Where(
                    dauthor => dauthor.id == author.id && dauthor.firstName == author.firstName && dauthor.lastName == author.lastName
                    ).Any();

                if (isOld || !this.DoesExist(author.firstName, author.lastName))
                {
                    //try catch blok zbog utrkivanja
                    try
                    {
                        _context.Update(author);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!AuthorExists(author.id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("", "Author already exists");
                }
            }
            return(View(author));
        }