gpt4 book ai didi

c# - 存储更新、插入或删除语句影响了意外的行数 (0)

转载 作者:行者123 更新时间:2023-11-30 20:44:41 24 4
gpt4 key购买 nike

我尝试更新我的 Products 表,但我不能,因为会抛出错误。

这是我的硬编码 Controller :

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

我使用强类型 View :

@model MyTechStore.Models.ProductsViewModel

我在 View 中添加:

@Html.HiddenFor(model => model.ProductId)

当我启动应用程序并输入一些数据以更新现有数据并按保存时,抛出异常:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

当我调试时,我看到只有 ProductId 为 0。其他一切正常。我用脚手架 Controller 进行了测试,但没问题。我想使用 View 模型,而不是因为脚手架 Controller 使用我的数据库中的模型。

谁能告诉我哪里错了?

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

最佳答案

您需要在 ProductsViewModel 中填充 ProductId

public ProductsViewModel EditProduct(int? id)
{
TechStoreEntities context = new TechStoreEntities();

Product dbProduct = context.Products.Find(id);

ProductsViewModel product = new ProductsViewModel()
{
// You need this line to pass the value to View
ProductId = dbProduct.ProductId,

Name = dbProduct.Name,
Price = dbProduct.Price,
Quantity = dbProduct.Quantity,
CategoryID = dbProduct.CategoryID,
SubcategoryID = dbProduct.SubcategoryID,
IsNew = dbProduct.IsNew
};

return product;
}

关于c# - 存储更新、插入或删除语句影响了意外的行数 (0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com