ASP.NET MVC 2 UpdateModel() is not updating values in memory or database

☆樱花仙子☆ 提交于 2019-12-22 22:22:19

问题


I am new to MVC, and so am working through the NerdDinner tutorial, here. In particular, I'm running into problems with the use of the UpdateModel method, which is explained in the part five of that tutorial. The problem is, when I try to edit the value of a dinner object using the UpdateModel method, the values do not get updated, and no exceptions are thrown.

Oddly, I am not having any trouble with the Create or Delete features that are illustrated in the tutorial. Only the update feature isn't working.

Below, I have included the Controller code that I am using, as well as the view markup, which is contained in both an aspx View file and an ascx Partial View file.

Here is the code inside my Controller, called DinnerController.cs:

    //
    // GET: /Dinners/Edit/2
    [Authorize]
    public ActionResult Edit(int id)
    {

        Dinner dinner = dinnerRepository.GetDinner(id);

        return View(new DinnerFormViewModel(dinner)); 
    }

    //
    // POST: /Dinners/Edit/2
    [AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Edit(int id, FormCollection formValues)
    {

        Dinner dinner = dinnerRepository.GetDinner(id);

        try
        {
            UpdateModel(dinner);
            var x = ViewData.GetModelStateErrors(); // <-- to catch other ModelState errors

            dinnerRepository.Save();

            return RedirectToAction("Details", new { id = dinner.DinnerID });
        }
        catch
        {

            ModelState.AddRuleViolations(dinner.GetRuleViolations());

            return View(new DinnerFormViewModel(dinner)); 
        }
    }

The line with the comment "to catch other ModelState errors" was added after reading a possible solution from another StackOverflow thread, here:

ASP.NET MVC Updatemodel not updating but not throwing error

Unfortunately, that solution didn't 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 my DinnerForm.ascx Partial View. This Partial View file is also used by the Create feature, which is working 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've been hitting away at this for hours, and I'm out of ideas. So, I'm hoping someone here can help nudge me in the right direction, in order to figure out what I'm doing wrong.


回答1:


You got something mixed up. You are sending DinnerFormViewModel to View but trying to receive Dinner.Change your post method like this:

[AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Edit(int id, FormCollection formValues)
    {

        var dinner=new DinnerFormViewModel(dinnerRepository.GetDinner(id));

        try
        {
            UpdateModel(dinner);
            var x = ViewData.GetModelStateErrors(); // <-- to catch other ModelState errors

            dinnerRepository.Save();

            return RedirectToAction("Details", new { id = dinner.Dinner.DinnerID });
        }
        catch
        {

            ModelState.AddRuleViolations(dinner.GetRuleViolations());

            return View(new DinnerFormViewModel(dinner)); 
        }
    }

There may be something I missed here, don't remember DinnerFormViewModel right now. Please check those

edit: Actually I realized this post doesn't solve the problem really. The code posted in the question works for me. There is a problem but but not here.




回答2:


dinnerRepository.Save() is the code the actually updates the database. What UpdateModel(dinner) does is extract the values from the form collection and put them in your dinner object.




回答3:


Just in case it helps someone else in the future, the problem here was not necessarily due to the use of a DinnerFormViewModel, as I suspected. Rather, the problem was with the use of strongly-typed helper methods, such as Html.TextBoxFor and the way I was invoking the UpdateModel method.

This problem and it's solution is explained in detail in another thread in StackOverflow, here.



来源:https://stackoverflow.com/questions/2936927/asp-net-mvc-2-updatemodel-is-not-updating-values-in-memory-or-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!