仅更新模型的一部分

发布于 2024-11-11 15:08:11 字数 1152 浏览 0 评论 0原文

我正在使用 ASP.NET MVC 3 和实体框架代码优先。我有一个页面(使用 Razor View Engine),允许用户更新模型(产品)的部分内容:

@Html.LabelFor(model => model.Overview) @Html.TextAreaFor(model => model.Overview)

@Html.LabelFor(model => model.Description)
@Html.TextAreaFor(model => model.Description)

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

我的控制器方法如下所示:

[HttpPost]
public ActionResult Update(Product product)
{
  db.Products.Attach(product);
  db.SaveChanges();
}

我想要做的就是更新 Product 模型的 Overview 和 Description 属性。但是,当我运行代码时,模型不会在数据库中更新,并且我没有收到任何错误。

当我在调试时检查产品对象时,我发现虽然 ProductId、Overview 和 Description 字段是正确的(根据 FORM POST),但其他字段为 NULL(这是我所期望的)。

我想知道产品对象的不完整状态是否导致它无法保存到数据库?

这是模型:

公共类产品 { 公共 int ProductId { 获取;放; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [DataType(DataType.MultilineText)]
    public string Overview { get; set; }

    public int SupplierId { get; set; }
    public virtual Supplier Supplier { get; set; }
}

I'm using ASP.NET MVC 3 and Entity Framework Code First. I have a page (using Razor View Engine) which allows a user to update parts of a model (Product):

@Html.LabelFor(model => model.Overview)
@Html.TextAreaFor(model => model.Overview)

@Html.LabelFor(model => model.Description)
@Html.TextAreaFor(model => model.Description)

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

My controller method looks like this:

[HttpPost]
public ActionResult Update(Product product)
{
  db.Products.Attach(product);
  db.SaveChanges();
}

All I want to do is to update the Overview and Description attributes of the Product model. However, when I run the code the model is not updated in the database, and I don't get any errors.

When I inspect the product object whilst debugging, I see that whilst the ProductId, Overview and Description fields are correct (as per the FORM POST), the other fields are NULLs (which I would expect).

I'm wondering if the incomplete state of the product object is causing it not to save to the database?

Here is the model:

public class Product
{
public int ProductId { get; set; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [DataType(DataType.MultilineText)]
    public string Overview { get; set; }

    public int SupplierId { get; set; }
    public virtual Supplier Supplier { get; set; }
}

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

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

发布评论

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

评论(2

如梦初醒的夏天 2024-11-18 15:08:11

在进行编辑时,请尝试首先从数据库中选择一条现有记录(您要编辑的记录),然后使用从表单收集的值(即传递到控制器操作的模型)更新它,然后保存它。

例如

[HttpPost]
public ActionResult Update(Product productPassedInFromView)
{ 
    Product productToEdit = db.Products.Find(productPassedInFromView.ID);

    productToEdit.Property1 = productPassedInFromView.Property1;
    productToEdit.Property2 = productPassedInFromView.Property2;
    //Continue for all the fields you want to edit.

    db.SaveChanges();
}

When it comes to editing, try selecting an existing record from the database first (the one you want to edit), then update it with the values collected from your form (i.e. the model passed into your controller action), then save it.

E.g.

[HttpPost]
public ActionResult Update(Product productPassedInFromView)
{ 
    Product productToEdit = db.Products.Find(productPassedInFromView.ID);

    productToEdit.Property1 = productPassedInFromView.Property1;
    productToEdit.Property2 = productPassedInFromView.Property2;
    //Continue for all the fields you want to edit.

    db.SaveChanges();
}
§对你不离不弃 2024-11-18 15:08:11

如果您知道要更新实体的哪些属性,则可以使用 EF 中的 ChangeTracker 仅将这些属性标记为已更改。下面是来自优秀书籍[编程实体框架:DbContext](http://shop.oreilly.com/product/0636920022237.do)的修改示例:

db.Products.Attach(product);
var entry = db.Entry(product);
entry.State = EntityState.Unchanged;
entity.Property(p => p.Overview).IsModified = true;
entity.Property(p => p.Description).IsModified = true;
db.SaveChanges();

这将为您节省往返数据库的时间。但当然,只有当您知道哪些属性正在更改时它才有效。还有一些其他方法可以实现这一目标,但这是最简单的。

If you know which properties of entity you will update, you can use ChangeTracker in EF to mark only those properties as changed. Below is a modified example from excellent book [Programming Entity Framework: DbContext] (http://shop.oreilly.com/product/0636920022237.do):

db.Products.Attach(product);
var entry = db.Entry(product);
entry.State = EntityState.Unchanged;
entity.Property(p => p.Overview).IsModified = true;
entity.Property(p => p.Description).IsModified = true;
db.SaveChanges();

This will save you a roundtrip to database. But of course it works only when you know which properties are changing. There are some other ways to achieve this, but this one is most straightforward.

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