A
A
AlkesPro2020-05-13 00:57:07
ASP.NET
AlkesPro, 2020-05-13 00:57:07

Instead of changing the object, a new entry is added to the table. How to fix?

I have code to change the data of an object, but instead of updating the current object's information, it creates a new object. How to implement update of the transferred object in the table?

public async Task<IActionResult> EditBook(int? id)
        {
            if (id != null)
            {
                var book = db.Books
                        .Include(c => c.User)
                        .AsNoTracking()
                        .Single(i => id == i.m_ID);
                if (book != null)
                    return View(book);
            }
            return NotFound();
        }
        [HttpPost]
        public async Task<IActionResult> EditBook(Book book)
        {
             if (count_of_books(book) < 4)
             {
                db.Books.UpdateRange(book);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
             }
             else
             {
                return Redirect($"ErrUserHaveBooksOverLimit?id={book.UserID}");
            }
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question