我正在使用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();
}
所有我想要做的是更新产品型号的概述和说明属性。但是,当我运行代码时,模型不会在数据库中更新,而且也不会出现任何错误。当我在调试时检查产品对象时,我发现虽然ProductId,Overview和Description字段是正确的(按照FORM POST),但其他字段是NULL(我期望)。
我想知道产品对象的不完整状态是否导致它不保存到数据库?
这里是模型:
公共类产品 { 公众诠释产品编号{获得;组; }
[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; }
}