仅更新模型的一部分
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在进行编辑时,请尝试首先从数据库中选择一条现有记录(您要编辑的记录),然后使用从表单收集的值(即传递到控制器操作的模型)更新它,然后保存它。
例如
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.
如果您知道要更新实体的哪些属性,则可以使用 EF 中的 ChangeTracker 仅将这些属性标记为已更改。下面是来自优秀书籍[编程实体框架:DbContext](http://shop.oreilly.com/product/0636920022237.do)的修改示例:
这将为您节省往返数据库的时间。但当然,只有当您知道哪些属性正在更改时它才有效。还有一些其他方法可以实现这一目标,但这是最简单的。
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):
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.