【问题标题】:ASP.NET MVC 2 UpdateModel() is not updating values in memory or databaseASP.NET MVC 2 UpdateModel() 未更新内存或数据库中的值
【发布时间】:2010-05-29 23:18:57
【问题描述】:

我是 MVC 新手,因此正在学习 NerdDinner 教程,here。特别是,我在使用 UpdateModel 方法时遇到了问题,该教程的第五部分对此进行了解释。问题是,当我尝试使用 UpdateModel 方法编辑晚餐对象的值时,值不会更新,也不会引发异常。

奇怪的是,我对教程中说明的创建或删除功能没有任何问题。只有更新功能不起作用。

下面,我已经包含了我正在使用的控制器代码,以及视图标记,它包含在一个 aspx 视图文件和一个 ascx 部分视图文件中。

这是我的 Controller 中的代码,名为 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)); 
        }
    }

在从另一个 StackOverflow 线程读取可能的解决方案后,添加了注释“捕获其他 ModelState 错误”,此处:

ASP.NET MVC Updatemodel not updating but not throwing error

很遗憾,该解决方案对我没有帮助。

这是我的 Dinners/Edit.aspx 视图中的相应标记:

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit Dinner</h2>

    <% Html.RenderPartial("DinnerForm"); %>

</asp:Content>

这是我的 DinnerForm.ascx 部分视图中的相应标记。 这个部分视图文件也被创建功能使用,工作正常

<%=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>

<% } %>

无论如何,我已经为此努力了好几个小时,但我没有想法。所以,我希望这里的人可以帮助我朝着正确的方向前进,以便找出我做错了什么。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-2 updatemodel nerddinner


    【解决方案1】:

    dinnerRepository.Save() 是实际更新数据库的代码。 UpdateModel(dinner) 所做的是从表单集合中提取值并将它们放入您的 dinner 对象中。

    【讨论】:

    • 感谢您的回复。您对晚餐Repository.Save() 方法和UpdateModel(dinner) 方法之间的关系是正确的。我知道,我只是不太了解,无法在上面的帖子中阐明这种区别:)
    【解决方案2】:

    你搞错了。您正在将 DinnerFormViewModel 发送到 View 但尝试接收 Dinner。像这样更改您的发布方法:

    [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)); 
            }
        }
    

    我可能在这里错过了一些东西,现在不记得 DinnerFormViewModel。请检查那些

    编辑:实际上我意识到这篇文章并没有真正解决问题。问题中发布的代码对我有用。有问题,但不是这里。

    【讨论】:

    • 你说得对,它确实与我使用 DinnerFormViewModel 类有关。我通过摆脱使用该类而只使用 Model.Dinner 对象来验证这一点。当我以这种方式回滚代码时,更新工作正常。不过,我真的很想在这里使用 ViewModel,所以我希望我知道我对它的使用是错误的。您是否碰巧对我如何更好地实施它有更多的了解?或者,你有没有想过我可能会如何滥用它?
    • 我不确定。我认为视图的强类型有问题。局部视图、编辑视图和创建视图的强类型是什么?
    • 我认为,在 Partial View 中强类型化的唯一东西是 Model,它是一个 DinnerFormViewModel 对象,现在是一个 Dinner 对象。无论如何,再次感谢您的帮助。如果我偶然发现有关这方面的更多信息,我将返回并使用更多详细信息更新此线程。谢谢!
    【解决方案3】:

    以防万一它在将来对其他人有所帮助,这里的问题不一定是由于使用了 DinnerFormViewModel,正如我所怀疑的那样。相反,问题在于使用强类型辅助方法,例如 Html.TextBoxFor 以及我调用 UpdateModel 方法的方式。

    StackOverflow 的另一个线程here 中详细解释了这个问题及其解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多