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; }
}