如何更新使用MVC2 RC2

编程入门 行业动态 更新时间:2024-10-21 19:38:06
本文介绍了如何更新使用MVC2 RC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想编辑记录。我有缺省路由。结果当我点击提交按钮我上的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

本文发布于:2023-10-09 06:12:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!