I am new to MVC, so working on the NerdDinner tutorial here . In particular, I am having problems using the UpdateModel method, which is explained in the fifth part of this tutorial. The problem is that when I try to change the value of a dinner object using the UpdateModel method, the values are not updated, and no exceptions occur.
Oddly enough, I have no problems with the Create or Delete functions, which are illustrated in the tutorial. Only the update function does not work.
Below I have included the controller code that I am using, as well as the markup of the view, which is contained both in the aspx View file and in the file with the partial ascx view.
Here is the code inside my controller called DinnerController.cs:
[Authorize]
public ActionResult Edit(int id)
{
Dinner dinner = dinnerRepository.GetDinner(id);
return View(new DinnerFormViewModel(dinner));
}
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
var x = ViewData.GetModelStateErrors();
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
}
a line with the comment “catch other ModelState errors” was added after reading a possible solution from another StackOverflow stream:
ASP.NET MVC Updatemodel does not update, but does not throw an error
Unfortunately, this solution did not help me.
Here is the corresponding markup in my Dinners / Edit.aspx view:
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>
Here is the corresponding markup in a partial view of DinnerForm.ascx. This Partial View file is also used by the Create function, which works just fine :
<%=Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<label for="Title">Dinner Title:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Title)%>
<%=Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="EventDate">EventDate:</label>
<%=Html.TextBoxFor(model => Model.Dinner.EventDate, new { value = String.Format("{0:g}", Model.Dinner.EventDate) })%>
<%=Html.ValidationMessage("EventDate", "*") %>
</p>
<p>
<label for="Description">Description:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Description)%>
<%=Html.ValidationMessage("Description", "*")%>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Address)%>
<%=Html.ValidationMessage("Address", "*") %>
</p>
<p>
<label for="Country">Country:</label>
<%=Html.DropDownListFor(model => Model.Dinner.Country, Model.Countries)%>
<%=Html.ValidationMessage("Country", "*") %>
</p>
<p>
<label for="ContactPhone">ContactPhone #:</label>
<%=Html.TextBoxFor(model => Model.Dinner.ContactPhone)%>
<%=Html.ValidationMessage("ContactPhone", "*") %>
</p>
<p>
<label for="Latitude">Latitude:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Latitude)%>
<%=Html.ValidationMessage("Latitude", "*") %>
</p>
<p>
<label for="Longitude">Longitude:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Longitude)%>
<%=Html.ValidationMessage("Longitude", "*") %>
</p>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
<% } %>
In any case, I wander for hours at this time, and I have no ideas. So, I hope that someone here can help push me in the right direction to understand what I'm doing wrong.