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

标签 c# asp.net-mvc entity-framework

我尝试更新我的产品表,但无法更新,因为抛出错误。

这是我的硬编码 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/

相关文章:

c# - 如何在进入 Controller 之前调试 .net 中的挂起请求

c# - 在 C# Unity 中获取实时视频流

c# - Windows 身份验证并通过数据库添加授权角色 - MVC asp.net

c# - 在 Entity Framework 和 Sql Server 中创建对象计数器

c# - 在 Where LINQ 查询中使用 Contains()

c# - Hangfire 配置和 Ninject 配置

c# - 派生抽象类 asp.net mvc 的 JSON 自定义 Binder null

asp.net-mvc - MVC3、Ninject、MvcSiteMapProvider - 如何将依赖项注入(inject)到重写方法

javascript - 图像未显示在我的 mvc 应用程序中

c# - 在 MVVM 模式中使用 ASP.NET MVC 中的 C# 扩展方法