Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6681149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:26+00:00 2026-05-26T04:33:26+00:00

I bought a book about MVC 3. In this book, there is an example

  • 0

I bought a book about MVC 3. In this book, there is an example for the implementation of an edit method. Below is the code:

[HttpPost]
public ActionResult Edit(Product product) {
    if (ModelState.IsValid) {
        TryUpdateModel(product); 
        repository.SaveProduct(product);
        return RedirectToAction("Index");
    } else {
        // there is something wrong with the data values
        return View(product);
    }
}

This code works very well for the creation of a newly product but didn’t work for the edition of an exisiting product.

I updated the code for successfully editing a product (see code below):

[HttpPost]
public ActionResult Edit(Product prod)
{
    if (ModelState.IsValid)
    {
        Product product = repository.Products.FirstOrDefault(p => p.ProductID == prod.ProductID);
        TryUpdateModel(product);
        repository.SaveProduct(product);
        return RedirectToAction("Index");
    }
    else
    {
        // there is something wrong with the data values
        return View(prod);
    }
}

EDIT

Here is the Saveproduct method:

public void SaveProduct(Product product)
{
    if (product.ProductID == 0)
    {
        context.Products.Add(product);
    }         
    context.SaveChanges();
}

As you can see, the modification I do in the code is about retrieving the product based on his ID, then save the product.

My question: why the second method works and not the first one? In the second method, why do we have to retrieve the product from the repository?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:33:27+00:00Added an answer on May 26, 2026 at 4:33 am

    You don’t need the extra TryUpdateModel method call because MVC has already updated your model when you receive it as a parameter in your Edit method.

    [HttpPost]
    public ActionResult Edit(Product product) {
        if (ModelState.IsValid) {
            repository.SaveProduct(product);
            return RedirectToAction("Index");
        } else {
            // there is something wrong with the data values
            return View(product);
        }
    }
    

    Your SaveProduct method does not handle the editing of a detached entity correctly.

    public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
           var entry = context.Entry(product);
           entry.State = EntityState.Modified;
        }         
        context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

helo everyone This is a code from a book i have bought. It gives
In order to learn more about Spring.NET, I bought the book Spring in Action,
I have tried to work this out myself (even bought a Kindle book!), but
I bought a book on Amazon which was meant to prepare me for 70-536
I recently bought Bob's book Delphi XML, SOAP & Web Services in page 85
I bought the O'Reilly Dojo book by Matthew Russell. It says in your toolkit
I bought this program that created a pretty nice imageuploader flash script however I
I have bought a book (Beginning Android Games) by Mario Zechner. On page 149,
I just bought the book: Sams Teach Yourself iPhone Application Development in 24 Hours.
I just bought a book C Interfaces and Implementations. in chapter one , it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.