EditorForModel Issuehttp://forums.asp.net/t/1787469.aspx/1?EditorForModel+IssueFri, 30 Mar 2012 23:04:55 -040017874694908975http://forums.asp.net/p/1787469/4908975.aspx/1?EditorForModel+IssueEditorForModel Issue <p>Hello Everyone,</p> <p>I am having an issue with using HTml.EditorForModel. When I first started developing an app I used the Html helper to make a form to fill some data. As the project went on I decided the way that EditorForModel didn't display the form in a very user friendly way (There is about 10 or 11 fileds to fill out some of them multi line text boxes so it's kind of long and requires some scrolling). I coded each field individually so I could split them up into a 2 column layout with a section on the bottom that spans both columns.</p> <p>Once I made the change I noticed that when I tried to updat an existing record it would save the changes as a new record so I would end up with before and after copies of the record I tried to update. Does anyone have any idea why this is? I don't particularly care which method I go with as long as I can use the same layout I created by individually crating the editors. Here is the code for the view and the methods used to save the changes, hopefully they can shed some light on the situation:</p> <pre class="prettyprint">@model StatusBoard.Domain.Entities.Issue @{ ViewBag.Title = &quot;Edit &quot; &#43; @Model.OrderNumber; Layout = &quot;~/Views/Shared/_Layout.cshtml&quot;; } &lt;h2&gt;Edit @Model.OrderNumber&lt;/h2&gt; @using (Html.BeginForm(&quot;Edit&quot;, &quot;Issue&quot;)) { @*@Html.EditorForModel()*@ &lt;div id=&quot;columns&quot; class=&quot;over-hide&quot;&gt; @Html.Hidden(&quot;Issue ID&quot;, Model.IssueID) &lt;div id=&quot;left-col&quot;&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.OrderNumber, &quot;Order Number&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.OrderNumber) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.OrderDate, &quot;Order Date&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.OrderDate) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.ItemName, &quot;Item Name&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.ItemName) &lt;/div&gt; &lt;/div&gt;&lt;!-- /left-col --&gt; &lt;div id=&quot;right-col over-hide&quot;&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.RequestDate, &quot;Date of Request&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.RequestDate) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.RequestedBy, &quot;Requested By:&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.RequestedBy) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.ATTN, &quot;ATTN:&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.ATTN) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.CSRequest, &quot;CS Request&quot;) &lt;br /&gt; @Html.DropDownListFor(x =&gt; x.CSRequest, new[] { new SelectListItem() {Text = &quot;No Movement&quot;, Value = &quot;No Movement&quot;}, new SelectListItem() {Text = &quot;No Tracking&quot;, Value = &quot;No Tracking&quot;}, new SelectListItem() {Text = &quot;Missing Item&quot;, Value = &quot;Missing Item&quot;}, new SelectListItem() {Text = &quot;Defective&quot;, Value = &quot;Defective&quot;}, new SelectListItem() {Text = &quot;Item Not As Described/Wrong Item&quot;, Value = &quot;Item Not As Described/Wrong Item&quot;}, new SelectListItem() {Text = &quot;Cancel/Refund&quot;, Value = &quot;Cancel/Refund&quot;}, new SelectListItem() {Text = &quot;See Notes&quot;, Value = &quot;See Notes&quot;}, }, &quot;Choose an option&quot;) &lt;/div&gt; &lt;/div&gt;&lt;!-- /right-col --&gt; &lt;/div&gt;&lt;!-- /columns --&gt; &lt;div id=&quot;center-col&quot;&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.ActionTaken, &quot;Actions Taken&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.ActionTaken) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.TrackingNumber, &quot;Tracking Number:&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.TrackingNumber) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; @Html.LabelFor(x =&gt; x.Notes, &quot;Notes&quot;) &lt;br /&gt; @Html.EditorFor(x =&gt; x.Notes) &lt;/div&gt; &lt;div class=&quot;edit-field&quot;&gt; &lt;h3&gt;@Html.LabelFor(x =&gt; x.Resolved, &quot;Issue Resolved: &quot;)&lt;/h3&gt; @Html.CheckBoxFor(x =&gt; x.Resolved) &lt;/div&gt; &lt;/div&gt;&lt;!-- /center-col --&gt; &lt;input type=&quot;submit&quot; value=&quot;Update&quot; /&gt; @Html.ActionLink(&quot;Cancel and Return to List&quot;, &quot;List&quot;) }</pre> <p><br />The C# code:</p> <pre class="prettyprint"> [HttpPost] public ActionResult Edit(Issue issue) { EfDbContext context = new EfDbContext(); if (ModelState.IsValid) { repository.SaveIssue(issue); TempData["message"] = string.Format("{0} has been updated", issue.OrderNumber); return RedirectToAction("List"); } else { //There is something wrong with data values return View(issue); } }</pre> <pre class="prettyprint"> public void SaveIssue(Issue issue) { if (issue.IssueID == 0) { context.Issues.Add(issue); } else { context.Issues.Attach(issue); context.Entry(issue).State = EntityState.Modified; } context.SaveChanges(); }</pre> <p>Thanks for help in advance.<br> <br> <br> </p> 2012-03-30T21:26:44-04:004908996http://forums.asp.net/p/1787469/4908996.aspx/1?Re+EditorForModel+IssueRe: EditorForModel Issue <p>Have you used the debugger to make sure the if statement that checks for the ID is actually getting the id?</p> 2012-03-30T23:02:10-04:004908999http://forums.asp.net/p/1787469/4908999.aspx/1?Re+EditorForModel+IssueRe: EditorForModel Issue <p>try changing</p> <pre class="prettyprint">@Html.Hidden(&quot;Issue ID&quot;, Model.IssueID) to</pre> <pre class="prettyprint"><span class="pln">@Html.Hidden("IssueID", Model.IssueID)</span></pre> <pre class="prettyprint"><span class="pln"><br /></span></pre> 2012-03-30T23:04:55-04:00