ViewModel 和创建/编辑操作出错

发布于 2024-09-26 17:17:54 字数 2313 浏览 3 评论 0原文

我正在尝试按照 NerdDinner 教程使用 Northwind 数据库创建 ASP.NET MVC 2 Web 应用程序,但现在在尝试编辑产品时不断收到以下错误:

“Supplier”类型的对象的成员“SupplierID”的值已更改。 定义对象身份的成员不能更改。 考虑添加具有新标识的新对象并删除现有对象。

仅当我更改类别和/或供应商(均为 DropDownLists)时才会发生这种情况,其他字段(复选框和文本框)都可以。

我也无法创建新产品,因为 Model.IsValid 由于某种原因总是返回 false(无例外)。

我做错了什么?

ProductController.cs

    public ActionResult Edit(int id) {
        Product productToEdit = productsRepository.Get(id);

        return View(new ProductViewModel(productToEdit));
    }

    [HttpPost]
    public ActionResult Edit(int id, FormCollection formValues) {
        Product productToEdit = productsRepository.Get(id);

        if (TryUpdateModel(productToEdit, "Product")) {
            productsRepository.Save();
            return RedirectToAction("Details", new { id = productToEdit.ProductID });
        }

        return View(productToEdit);
    }

ProductViewModel.cs

public class ProductViewModel {

    public Product Product { get; private set; }
    public SelectList Suppliers { get; private set; }
    public SelectList Categories { get; private set; }

    public ProductViewModel(Product product) {
        this.Product = product;

        this.Suppliers = new SelectList(new SuppliersRepository()
            .GetAllSuppliers()
            .Select(s => new SelectListItem {
                Text = s.CompanyName,
                Value = s.SupplierID.ToString()
            }), "Value", "Text");

        this.Categories = new SelectList(new CategoriesRepository()
            .GetAllCategories()
            .Select(c => new SelectListItem {
                Text = c.CategoryName,
                Value = c.CategoryID.ToString()
            }), "Value", "Text");
    }
}

ProductForm.ascx

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Product.SupplierID) %>
        </div>

        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.Product.Supplier.SupplierID, Model.Suppliers) %>
        </div>

当然,这些代码只是各个Controller和View的摘录。 ProductViewModel 是完整的代码。我省略了 ProductRepository 类。

I'm trying to create a ASP.NET MVC 2 webapp using the Northwind database following the NerdDinner tutorial, but now I keep getting the following error when trying to EDIT a product:

Value of member 'SupplierID' of an object of type 'Supplier' changed.
A member defining the identity of the object cannot be changed.
Consider adding a new object with new identity and deleting the existing one instead.

This happens only when I change the Category and/or Suppliers (both are DropDownLists), the other fields (checkbox and textbox) are ok.

I also can't CREATE a new product since the Model.IsValid always return false for some reason (no exceptions).

What am I doing wrong?

ProductController.cs

    public ActionResult Edit(int id) {
        Product productToEdit = productsRepository.Get(id);

        return View(new ProductViewModel(productToEdit));
    }

    [HttpPost]
    public ActionResult Edit(int id, FormCollection formValues) {
        Product productToEdit = productsRepository.Get(id);

        if (TryUpdateModel(productToEdit, "Product")) {
            productsRepository.Save();
            return RedirectToAction("Details", new { id = productToEdit.ProductID });
        }

        return View(productToEdit);
    }

ProductViewModel.cs

public class ProductViewModel {

    public Product Product { get; private set; }
    public SelectList Suppliers { get; private set; }
    public SelectList Categories { get; private set; }

    public ProductViewModel(Product product) {
        this.Product = product;

        this.Suppliers = new SelectList(new SuppliersRepository()
            .GetAllSuppliers()
            .Select(s => new SelectListItem {
                Text = s.CompanyName,
                Value = s.SupplierID.ToString()
            }), "Value", "Text");

        this.Categories = new SelectList(new CategoriesRepository()
            .GetAllCategories()
            .Select(c => new SelectListItem {
                Text = c.CategoryName,
                Value = c.CategoryID.ToString()
            }), "Value", "Text");
    }
}

ProductForm.ascx

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Product.SupplierID) %>
        </div>

        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.Product.Supplier.SupplierID, Model.Suppliers) %>
        </div>

Of course, these codes are only excerpts of each Controller and Views. The ProductViewModel is the complete code. I omited the ProductRepository class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-10-03 17:17:54

在控制器操作方法中放置一个断点并读取 modelstate 对象。检查每个按键是否有错误。错误的描述会有帮助。在此之前尝试

<div class="editor-field">
            <%= Html.DropDownListFor(model => model.SupplierID, Model.Suppliers) %>
        </div>

这就是我在 L2S 中通过列表框编辑外键值时所做的事情。不确定你是否使用 EF。

put a break point in ur controller action method and read the modelstate object. inspect each key to check if there is an error. the description of error will help. before that try

<div class="editor-field">
            <%= Html.DropDownListFor(model => model.SupplierID, Model.Suppliers) %>
        </div>

this is what i do when editing foreign key value through listbox in L2S. not sure if u r using EF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文