【发布时间】:2011-09-06 06:03:23
【问题描述】:
我首先使用 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; }
}
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-3