【发布时间】:2015-03-10 00:21:03
【问题描述】:
我尝试更新我的 Products 表,但我不能因为抛出错误。
这是我的硬编码控制器:
[HttpPost]
public ActionResult EditProduct(ProductsViewModel productViewModel)
{
TechStoreEntities context = new TechStoreEntities();
Product newProduct = new Product
{
ProductId = productViewModel.ProductId,
Name = productViewModel.Name,
Price = productViewModel.Price,
Discount = productViewModel.Discount,
Quantity = productViewModel.Quantity,
Size = productViewModel.Size,
Description = productViewModel.Description,
ProducerName = productViewModel.ProducerName,
PaymentMethods = productViewModel.PaymentMethods,
CategoryID = productViewModel.CategoryID,
SubcategoryID = productViewModel.SubcategoryID,
IsNew = productViewModel.IsNew,
IsEnable = productViewModel.IsEnable
};
context.Entry(newProduct).State = EntityState.Modified;
context.SaveChanges();
ViewBag.CategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID == null), "CategoryID", "Name");
ViewBag.SubcategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID != null), "CategoryID", "Name");
return RedirectToAction("Products");
}
这是一个模型:
public class ProductsViewModel
{
public int ProductId { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public int Discount { get; set; }
public int Quantity { get; set; }
public string Size { get; set; }
public string Description { get; set; }
public string ProducerName { get; set; }
public string PaymentMethods { get; set; }
public bool IsNew { get; set; }
public bool IsEnable { get; set; }
public string Category { get; set; }
public int CategoryID { get; set; }
public string Subcategory { get; set; }
public int SubcategoryID { get; set; }
public DateTime? CreateDate { get; set; }
}
我使用强类型视图:
@model MyTechStore.Models.ProductsViewModel
我在视图中添加:
@Html.HiddenFor(model => model.ProductId)
当我启动应用程序并输入一些数据以更新现有数据并按保存时,抛出异常:
System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
当我调试时,我看到只有 ProductId 为 0。其他一切正常。我用脚手架控制器测试过,但没问题。我想使用视图模型,而不是像脚手架控制器那样使用我数据库中的模型。
谁能告诉我哪里错了?
我的 GET 方法:
public ActionResult EditProduct(int? id)
{
TechStoreEntities context = new TechStoreEntities();
ProductsManipulate product = new ProductsManipulate();
ProductsViewModel editProduct = product.EditProduct(id);
ViewBag.CategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID == null), "CategoryID", "Name");
ViewBag.SubcategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID != null), "CategoryID", "Name");
return View(editProduct);
}
还有我的数据访问层:
public ProductsViewModel EditProduct(int? id)
{
TechStoreEntities context = new TechStoreEntities();
Product dbProduct = context.Products.Find(id);
ProductsViewModel product new ProductsViewModel
{
Name = dbProduct.Name,
Price = dbProduct.Price,
Quantity = dbProduct.Quantity,
CategoryID = dbProduct.CategoryID,
SubcategoryID = dbProduct.SubcategoryID,
IsNew = dbProduct.IsNew
};
return product;
}
【问题讨论】:
-
您能否在
EditProduct操作中显示您的[HttpGet] -
在编辑产品的时候,并不是要得到一个新产品,你要做的是在得到上下文之后,得到你要编辑的产品,比如 Product product = context.Products.Where(p => p.ProductId == productViewModel.ProductId) 然后像你现在做的那样将你的视图模型映射到这个现有的产品
标签: c# asp.net-mvc entity-framework