我想编辑记录。我有缺省路由。结果当我点击提交按钮我上的UpdateModel线异常:结果类型'MyProject.Mvc.Models.Product'的模式无法更新。结果在页面ProductID字段的验证提示值无效:结果值9是无效的。 9是我试图编辑记录的ID。 什么地方出错了?
公众的ActionResult编辑(INT ID){ 产品产品= productRepository.GetProduct(ID); 返回查看(新ProductFormViewModel(产品));}[HttpPost]公众的ActionResult编辑(INT ID,的FormCollection productFormViewModel){ 产品产品= productRepository.GetProduct(ID); 尝试 { // TODO:添加更新逻辑这里 的UpdateModel(产品,产品); productRepository.Save(); 返回RedirectToAction(「指数」); } 赶上(异常前) { 返回查看(新ProductFormViewModel(产品)); }}如果我更新示范线更改为:
的UpdateModel(产品);则不会抛出异常,并且不更新数据库中的数据。
我使用实体框架
命名空间MyProject.Mvc.Models{ [MetadataType(typeof运算(ProductMetaData))] 公共部分类产品 { 公共产品() { //初始化产品 this.CreateDate = System.DateTime.Now; } } 公共类ProductMetaData { [必需(的ErrorMessage =产品名称是必需)] [StringLength(50的ErrorMessage =产品名称必须在50个字符)] 公共对象产品名称{搞定;组; } [必需(的ErrorMessage =说明要求)] 公共对象描述{搞定;组; } } 公共类ProductFormViewModel { 公共产品产品{搞定;私人集; } 公共ProductFormViewModel() { 产品=新产品(); } 公共ProductFormViewModel(产品产品) { 产品=产品; } }}解决方案
你需要编辑的ID?如果ID是产品的PK在表那么它可能是一个具有约束力的问题。尝试
[MetadataType(typeof运算(ProductMetaData))][绑定(不包括=ID)]公共部分类产品{ 公共产品() { //初始化产品 this.CreateDate = System.DateTime.Now; }}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; } } }解决方案
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; } }
更多推荐
如何更新使用MVC2 RC2
发布评论