How to Update using MVC2 RC2
Asked Answered
A

3

2

I am trying to edit a record. I have the default route.
When I click the submit button I get an exception on the UpdateModel line:
The model of type 'MyProject.Mvc.Models.Product' could not be updated.
On the page the validation of the ProductId field is prompting the value is invalid:
The value '9' is invalid. 9 is the id of the record I am trying to edit. What could be wrong?

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));
   }
}

If I change the update model line to:

UpdateModel(product);

then no exception is thrown and the data is not updated in the database.

[Edit]

I am using Entity Framework

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;
      }
  }
}
Applecart answered 4/3, 2010 at 5:23 Comment(2)
I am using NerdDinner as a guide.Applecart
The problem lies in the Edit View, <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.ProductFormViewModel>" %> If I pass in the View model ProductFormViewModel it does not update but if I change it to <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.Product>" %> it updates.Applecart
A
0

Do you need to edit the ID? if the ID is the PK of the product in your table then it could be a binding issue. Try

[MetadataType(typeof(ProductMetaData))]
[Bind(Exclude="ID")]
public partial class Product
{
  public Product()
  {
      // Initialize Product
      this.CreateDate = System.DateTime.Now;
  }
}
Abbeyabbi answered 11/3, 2010 at 10:59 Comment(3)
I removed the Id from the View since I don't need to bind it but still the model does not update. I used the same code in MVC 1 and it updates. I think the problem lies in MVC 2. Perhaps there is a different way of updating. I am looking at NerdDinner and the latest ND has MVC 2 code but in my project the model does not updateApplecart
@Applecart - It doesn't matter if the ID is not on the View. The ModelBinder binds its anyway if you haven't explicitly excluded it with the Bind attribute. Why don't you try to exclude it as shown above and see what happens?Abbeyabbi
Nick, I tried as you mentioned as well and I am getting the same behavior. If I change the Edit View page directive from Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.ProductFormViewModel>" to Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.Product>" then the Model updates.Applecart
T
0

could you post your Model source code? does model have fields of class you want to update or just this class as object(Product)? the problem could exists becouse when your model has object Product, you should pass to UpdateModel method prefix with name of the class...

Terrell answered 4/3, 2010 at 7:25 Comment(0)
A
0

The problem with UpdateModel(product, "Product"); is that you are using the same prefix (Product) as the Product class name. Try using a different prefix. For this you might need to rename the Product property of the ProductFormViewModel class.

Audrit answered 6/3, 2010 at 9:12 Comment(0)
A
0

Do you need to edit the ID? if the ID is the PK of the product in your table then it could be a binding issue. Try

[MetadataType(typeof(ProductMetaData))]
[Bind(Exclude="ID")]
public partial class Product
{
  public Product()
  {
      // Initialize Product
      this.CreateDate = System.DateTime.Now;
  }
}
Abbeyabbi answered 11/3, 2010 at 10:59 Comment(3)
I removed the Id from the View since I don't need to bind it but still the model does not update. I used the same code in MVC 1 and it updates. I think the problem lies in MVC 2. Perhaps there is a different way of updating. I am looking at NerdDinner and the latest ND has MVC 2 code but in my project the model does not updateApplecart
@Applecart - It doesn't matter if the ID is not on the View. The ModelBinder binds its anyway if you haven't explicitly excluded it with the Bind attribute. Why don't you try to exclude it as shown above and see what happens?Abbeyabbi
Nick, I tried as you mentioned as well and I am getting the same behavior. If I change the Edit View page directive from Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.ProductFormViewModel>" to Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.Product>" then the Model updates.Applecart

© 2022 - 2024 — McMap. All rights reserved.