Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating only part of a model
    primarykey
    data
    text
    <p>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):</p> <p>@Html.LabelFor(model => model.Overview) @Html.TextAreaFor(model => model.Overview)</p> <pre><code>@Html.LabelFor(model =&gt; model.Description) @Html.TextAreaFor(model =&gt; model.Description) @Html.HiddenFor(model =&gt; model.ProductId) </code></pre> <p>My controller method looks like this:</p> <pre><code>[HttpPost] public ActionResult Update(Product product) { db.Products.Attach(product); db.SaveChanges(); } </code></pre> <p>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.</p> <p>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).</p> <p>I'm wondering if the incomplete state of the product object is causing it not to save to the database?</p> <p>Here is the model:</p> <p>public class Product { public int ProductId { get; set; }</p> <pre><code> [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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload