Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 4 Post data is resetting unchanged properties
    primarykey
    data
    text
    <p>What I am trying to make is a view in which the properties of a model are displayed, and the option to change 1 property, in this case "Status" for which I made a helper.</p> <p>I got to the point at which the changed property is correctly modified and changed, but somehow values of the other properties are now being resetted.</p> <p>I am guessing this is because the post data is being used to modify the model and since some properties are unchangeable, I guess an empty value is used to update the model?</p> <p>here is my controllers GET:</p> <pre><code>public ActionResult Edit(int id = 0) { ProductCode productcode = db.ProductCodes.Find(id); if (productcode == null) { return HttpNotFound(); } return View(productcode); } </code></pre> <p>and here the POST:</p> <pre><code>[HttpPost] public ActionResult Edit(ProductCode productcode) { if (ModelState.IsValid) { db.Entry(productcode).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(productcode); } </code></pre> <p>And finally the view:</p> <pre><code>@model PortalTMC.Models.ProductCode @{ ViewBag.Title = "Change status"; } &lt;h2&gt;Status veranderen&lt;/h2&gt; &lt;br /&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;ProductCode&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.Id) @Html.LabelFor(model =&gt; model.Id, "Id") @Html.DisplayFor(m =&gt; m.Id) @Html.LabelFor(model =&gt; model.Code, "Product code") @Html.DisplayFor(m =&gt; m.Code) @Html.LabelFor(model =&gt; model.Role, "Rol (1 = Administrator | 2 = Trainer | 3 = User)") @Html.DisplayFor(m =&gt; m.Role) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Status, "Status") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EnumDropDownListFor(model =&gt; model.Status) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Verander status" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Terug naar lijst", "Index") &lt;/div&gt; @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </code></pre> <p>Any help would be much appreciated</p> <p><strong>Update:</strong></p> <p>Unfortunately including or excluding the properties on the post method is not working :/ for example if I replace the post method to:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] [HttpPost] public ActionResult Edit([Bind(Exclude = "Role, Code")] ProductCode productcode) { if (ModelState.IsValid) { db.Entry(productcode).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(productcode); } </code></pre> <p>I still end up with Role and Code being reset..</p> <p>ProductCode is as follows:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace PortalTMC.Models { public class ProductCode { //PK [Key] public int Id { get; set; } public string Code { get; set; } public ProductCodeStatus Status { get; set; } public int Role { get; set; } } public enum ProductCodeStatus { Active, Inactive, Pending, } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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