我正在使用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; }
}