How to Update using MVC2 RC2

半城伤御伤魂 提交于 2019-12-12 19:02:08

问题


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

回答1:


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



回答2:


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...




回答3:


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.



来源:https://stackoverflow.com/questions/2377065/how-to-update-using-mvc2-rc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!