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.
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:
@model StatusBoard.Domain.Entities.Issue
@{
ViewBag.Title = "Edit " + @Model.OrderNumber;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit @Model.OrderNumber</h2>
@using (Html.BeginForm("Edit", "Issue"))
{
@*@Html.EditorForModel()*@
<div id="columns" class="over-hide">
@Html.Hidden("Issue ID", Model.IssueID)
<div id="left-col">
<div class="edit-field">
@Html.LabelFor(x => x.OrderNumber, "Order Number") <br />
@Html.EditorFor(x => x.OrderNumber)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.OrderDate, "Order Date") <br />
@Html.EditorFor(x => x.OrderDate)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.ItemName, "Item Name") <br />
@Html.EditorFor(x => x.ItemName)
</div>
</div><!-- /left-col -->
<div id="right-col over-hide">
<div class="edit-field">
@Html.LabelFor(x => x.RequestDate, "Date of Request") <br />
@Html.EditorFor(x => x.RequestDate)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.RequestedBy, "Requested By:") <br />
@Html.EditorFor(x => x.RequestedBy)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.ATTN, "ATTN:") <br />
@Html.EditorFor(x => x.ATTN)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.CSRequest, "CS Request") <br />
@Html.DropDownListFor(x => x.CSRequest, new[] {
new SelectListItem() {Text = "No Movement", Value = "No Movement"},
new SelectListItem() {Text = "No Tracking", Value = "No Tracking"},
new SelectListItem() {Text = "Missing Item", Value = "Missing Item"},
new SelectListItem() {Text = "Defective", Value = "Defective"},
new SelectListItem() {Text = "Item Not As Described/Wrong Item", Value = "Item Not As Described/Wrong Item"},
new SelectListItem() {Text = "Cancel/Refund", Value = "Cancel/Refund"},
new SelectListItem() {Text = "See Notes", Value = "See Notes"},
}, "Choose an option")
</div>
</div><!-- /right-col -->
</div><!-- /columns -->
<div id="center-col">
<div class="edit-field">
@Html.LabelFor(x => x.ActionTaken, "Actions Taken") <br />
@Html.EditorFor(x => x.ActionTaken)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.TrackingNumber, "Tracking Number:") <br />
@Html.EditorFor(x => x.TrackingNumber)
</div>
<div class="edit-field">
@Html.LabelFor(x => x.Notes, "Notes") <br />
@Html.EditorFor(x => x.Notes)
</div>
<div class="edit-field">
<h3>@Html.LabelFor(x => x.Resolved, "Issue Resolved: ")</h3>
@Html.CheckBoxFor(x => x.Resolved)
</div>
</div><!-- /center-col -->
<input type="submit" value="Update" />
@Html.ActionLink("Cancel and Return to List", "List")
}
The C# code:
[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);
}
}
imauld
Member
145 Points
85 Posts
EditorForModel Issue
Mar 30, 2012 09:26 PM|LINK
Hello Everyone,
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.
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:
@model StatusBoard.Domain.Entities.Issue @{ ViewBag.Title = "Edit " + @Model.OrderNumber; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Edit @Model.OrderNumber</h2> @using (Html.BeginForm("Edit", "Issue")) { @*@Html.EditorForModel()*@ <div id="columns" class="over-hide"> @Html.Hidden("Issue ID", Model.IssueID) <div id="left-col"> <div class="edit-field"> @Html.LabelFor(x => x.OrderNumber, "Order Number") <br /> @Html.EditorFor(x => x.OrderNumber) </div> <div class="edit-field"> @Html.LabelFor(x => x.OrderDate, "Order Date") <br /> @Html.EditorFor(x => x.OrderDate) </div> <div class="edit-field"> @Html.LabelFor(x => x.ItemName, "Item Name") <br /> @Html.EditorFor(x => x.ItemName) </div> </div><!-- /left-col --> <div id="right-col over-hide"> <div class="edit-field"> @Html.LabelFor(x => x.RequestDate, "Date of Request") <br /> @Html.EditorFor(x => x.RequestDate) </div> <div class="edit-field"> @Html.LabelFor(x => x.RequestedBy, "Requested By:") <br /> @Html.EditorFor(x => x.RequestedBy) </div> <div class="edit-field"> @Html.LabelFor(x => x.ATTN, "ATTN:") <br /> @Html.EditorFor(x => x.ATTN) </div> <div class="edit-field"> @Html.LabelFor(x => x.CSRequest, "CS Request") <br /> @Html.DropDownListFor(x => x.CSRequest, new[] { new SelectListItem() {Text = "No Movement", Value = "No Movement"}, new SelectListItem() {Text = "No Tracking", Value = "No Tracking"}, new SelectListItem() {Text = "Missing Item", Value = "Missing Item"}, new SelectListItem() {Text = "Defective", Value = "Defective"}, new SelectListItem() {Text = "Item Not As Described/Wrong Item", Value = "Item Not As Described/Wrong Item"}, new SelectListItem() {Text = "Cancel/Refund", Value = "Cancel/Refund"}, new SelectListItem() {Text = "See Notes", Value = "See Notes"}, }, "Choose an option") </div> </div><!-- /right-col --> </div><!-- /columns --> <div id="center-col"> <div class="edit-field"> @Html.LabelFor(x => x.ActionTaken, "Actions Taken") <br /> @Html.EditorFor(x => x.ActionTaken) </div> <div class="edit-field"> @Html.LabelFor(x => x.TrackingNumber, "Tracking Number:") <br /> @Html.EditorFor(x => x.TrackingNumber) </div> <div class="edit-field"> @Html.LabelFor(x => x.Notes, "Notes") <br /> @Html.EditorFor(x => x.Notes) </div> <div class="edit-field"> <h3>@Html.LabelFor(x => x.Resolved, "Issue Resolved: ")</h3> @Html.CheckBoxFor(x => x.Resolved) </div> </div><!-- /center-col --> <input type="submit" value="Update" /> @Html.ActionLink("Cancel and Return to List", "List") }The C# code:
[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); } }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(); }Thanks for help in advance.
RAZOR mvc
EnenDaveyBoy
Participant
1465 Points
1146 Posts
Re: EditorForModel Issue
Mar 30, 2012 11:02 PM|LINK
Have you used the debugger to make sure the if statement that checks for the ID is actually getting the id?
RAZOR mvc
EnenDaveyBoy
Participant
1465 Points
1146 Posts
Re: EditorForModel Issue
Mar 30, 2012 11:04 PM|LINK
try changing
@Html.Hidden("Issue ID", Model.IssueID) to@Html.Hidden("IssueID", Model.IssueID)