title: Edit method didn't works as explained in a popular book
body: <p>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:</p>
  
  <pre><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);
      }
  }
  </code></pre>
  
  <p>This code works very well for the creation of a newly product but didn't work for the edition of an exisiting product.</p>
  
  <p>I updated the code for successfully editing a product (see code below):</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(Product prod)
  {
      if (ModelState.IsValid)
      {
          Product product = repository.Products.FirstOrDefault(p =&gt; p.ProductID == prod.ProductID);
          TryUpdateModel(product);
          repository.SaveProduct(product);
          return RedirectToAction("Index");
      }
      else
      {
          // there is something wrong with the data values
          return View(prod);
      }
  }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>Here is the Saveproduct method:</p>
  
  <pre><code>public void SaveProduct(Product product)
  {
      if (product.ProductID == 0)
      {
          context.Products.Add(product);
      }         
      context.SaveChanges();
  }
  </code></pre>
  
  <p>As you can see, the modification I do in the code is about retrieving the product based on his ID, then save the product.</p>
  
  <p><strong>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?</strong></p>
  
  <p>Thanks.</p>