ASP.NET MVC 2的UpdateModel()没有在内存或更新数据库值 [英] ASP.NET MVC 2 UpdateModel() is not updating values in memory or database

查看:226
本文介绍了ASP.NET MVC 2的UpdateModel()没有在内存或更新数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC,所以我通过的NerdDinner教程的学习,这里。特别是,我运行到与使用的UpdateModel方法,这是该教程的第五部分解释的问题。问题是,当我尝试编辑使用的UpdateModel方法的晚宴对象的值,这些值没有得到更新,并没有异常抛出。

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.

下面,我已经包括控制器code,我使用,以及视图的标记,它同时包含在一个aspx文件查看和ASCX局部视图文件。

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.

下面是code我的控制器里面,叫DinnerController.cs:

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


从另一个计算器线程读取一个可能的解决方案,在此之后添加了注释搭上其他的ModelState错误行:

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

<一个href=\"http://stackoverflow.com/questions/1461283/asp-net-mvc-updatemodel-not-updating-but-not-throwing-error\">http://stackoverflow.com/questions/1461283/asp-net-mvc-updatemodel-not-updating-but-not-throwing-error

不幸的是,这种解决方案没有帮助我。

Unfortunately, that solution didn't help me.

下面是我的晚餐相应的标记/ Edit.aspx查看:

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>

下面是我DinnerForm.ascx管窥相应的标记。 此局部视图文件也被用于创建功能,这是工作的罚款

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.

推荐答案

您得到的东西混在一起。您发送DinnerFormViewModel查看,但尝试接收Dinner.Change您的文章的方法是这样的:

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

有可能是一些我错过这里,不记得DinnerFormViewModel现在。请检查这些

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

编辑:其实我发现这个职位并不能真正解决问题。张贴在问题的code为我工作。还有一个问题,但但不是在这里。

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.

这篇关于ASP.NET MVC 2的UpdateModel()没有在内存或更新数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆