Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Update using MVC2 RC2
    primarykey
    data
    text
    <p>I am trying to edit a record. I have the default route.<br> When I click the submit button I get an exception on the UpdateModel line:<br> <strong>The model of type 'MyProject.Mvc.Models.Product' could not be updated.</strong><br> On the page the validation of the ProductId field is prompting the value is invalid:<br> <strong>The value '9' is invalid.</strong> 9 is the id of the record I am trying to edit. <strong>What could be wrong?</strong></p> <pre><code>public ActionResult Edit(int id) { Product product = productRepository.GetProduct(id); return View(new ProductFormViewModel(product)); } [HttpPost] public ActionResult Edit(int id, FormCollection productFormViewModel) { Product product = productRepository.GetProduct(id); try { // TODO: Add update logic here UpdateModel(product, "Product"); productRepository.Save(); return RedirectToAction("Index"); } catch (Exception ex) { return View(new ProductFormViewModel(product)); } } </code></pre> <p>If I change the update model line to:</p> <pre><code>UpdateModel(product); </code></pre> <p>then no exception is thrown and the data is not updated in the database.</p> <p><strong>[Edit]</strong></p> <p>I am using Entity Framework</p> <pre><code>namespace MyProject.Mvc.Models { [MetadataType(typeof(ProductMetaData))] public partial class Product { public Product() { // Initialize Product this.CreateDate = System.DateTime.Now; } } public class ProductMetaData { [Required(ErrorMessage = "Product name is required")] [StringLength(50, ErrorMessage = "Product name must be under 50 characters")] public object ProductName { get; set; } [Required(ErrorMessage = "Description is required")] public object Description { get; set; } } public class ProductFormViewModel { public Product Product { get; private set; } public ProductFormViewModel() { Product = new Product(); } public ProductFormViewModel(Product product) { Product = product; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload