在MVC中,一个编辑HttpPost工作时,另一个不是。我在想什么? [英] In MVC, one Edit HttpPost is working, the other one isn't. What am I missing?

查看:87
本文介绍了在MVC中,一个编辑HttpPost工作时,另一个不是。我在想什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般和SO谷歌搜索并没有帮助我呢,所以:

Googling generally and on SO hasn't helped me yet, so:

我从头开始构建我的第一个MVC应用程序,由 MVC音乐商店例如去,但而不是建立在这里可以创建并取得相互争斗的舞台战士一个小应用程序。 (战士已经作出通过EF链接到基础表)。

I am building my first MVC application from scratch, going by the MVC Music Store example but instead building a little application where arena Fighters can be created and made to Fight each other. (Fighters and Fight have been made linked to underlying tables through EF).

我有两个战士战斗控制器。编辑的ActionResult 战斗是工作,但战士不是这样。当我打的按钮来保存我改变我回相关的索引页,但没有修改已经提交。这是我的问题:这是为什么失败

I have controllers for both the Fighters and the Fights. The Edit Actionresult for Fights is working, but for Fighters it is not. When I hit the button to save my alterations I return to the associated Index page, but no changes have been committed. This is my question: why is this failing?

BarracksController ,以及有故障的非更新HttpPost编辑(应该被命名为FighterController,但neverthemind):

From BarracksController, with the faulty non-updating HttpPost Edit (should have been named FighterController, but neverthemind):

        //
        // GET: /Barracks/Edit
        public ActionResult Edit(int id)
        {
            ViewData.Model = _FightDb.Fighters.Single(f => f.Id == id);
            return View();
        }


        //
        // POST: /Barracks/Edit
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var fighter = _FightDb.Fighters.Single(f => f.Id == id);

            try
            {
                UpdateModel(fighter, "Fighter");
                var x = ViewData.GetModelStateErrors();
                _FightDb.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = fighter;

                return View(viewModel);
            }

        }

(正如你看到的,我已经包括<一个href=\"http://stackoverflow.com/questions/2936927/asp-net-mvc-2-updatemodel-is-not-updating-values-in-memory-or-database\">the从这个GetModelStateErrors招SO质疑,但x的结果是

这是做工作,控制器FightController:

   //
        // GET: /Fights/Edit
        public ActionResult Edit(int id)
        {
            var viewModel = new FightDetailsViewModel
            {
                Fight = _FightDb.Fights.Single(f => f.ID == id),
                Fighters = _FightDb.Fighters.ToList()
            };

            return View(viewModel);
        }

        //
        // POST: /Fights/Edit
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var fight = _FightDb.Fights.Single(f => f.ID == id);

            try
            {
                UpdateModel(fight, "Fight");
                _FightDb.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new FightDetailsViewModel
                {
                    Fight = _FightDb.Fights.Single(f => f.ID == id),
                    Fighters = _FightDb.Fighters.ToList()
                };

                return View(viewModel);
            }
        }

这是战士edit.aspx:(评论后编辑)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3_EF_BW_Fight.Models.Fighter>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
    <h2>Edit</h2>

        <%: Html.EditorForModel()  %>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>

,它使用一共用了以下Fighter.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fighter>" %>
<% using (Html.BeginForm())
   {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fighter</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Id) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Id) %>
        <%: Html.ValidationMessageFor(model => model.Id) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FighterName) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FighterName) %>
        <%: Html.ValidationMessageFor(model => model.FighterName) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FighterStyleDescription) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FighterStyleDescription) %>
        <%: Html.ValidationMessageFor(model => model.FighterStyleDescription) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FighterLongDescription) %>
    </div>
    <div class="editor-field">
        <%: Html.TextAreaFor(model => model.FighterLongDescription) %>
        <%: Html.ValidationMessageFor(model => model.FighterLongDescription) %>
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<% } %>

这是战斗的edit.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3_EF_BW_Fight.ViewModels.FightDetailsViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">

    <h2>Edit</h2>
    <%: Html.EditorFor(model => model.Fight, new { Fighters = Model.Fighters })%>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>

这是Fight.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fight>" %>
<% using (Html.BeginForm())
   {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.ID) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.ID) %>
        <%: Html.ValidationMessageFor(model => model.ID) %>
    </div>


    <div class="editor-label">
        <%: Html.LabelFor(model => model.FightName) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FightName) %>
        <%: Html.ValidationMessageFor(model => model.FightName) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Fighter1ID) %><br />
        <%: Html.LabelFor(model => model.Fighter1Reference.Value.FighterName)%>
    </div>
    <div class="editor-field">
        <%: Html.DropDownList("Fighter1ID", new SelectList(ViewData["Fighters"] as IEnumerable, "ID", "FighterName", Model.Fighter1ID))%>
        <%: Html.ValidationMessageFor(model => model.Fighter1ID) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Fighter2ID) %>
    </div>
    <div class="editor-field">
        <%: Html.DropDownList("Fighter1ID", new SelectList(ViewData["Fighters"] as IEnumerable, "ID", "FighterName", Model.Fighter1ID))%>
        <%: Html.ValidationMessageFor(model => model.Fighter2ID) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Fighter1Login) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Fighter1Login) %>
        <%: Html.ValidationMessageFor(model => model.Fighter1Login) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Fighter2Login) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Fighter2Login) %>
        <%: Html.ValidationMessageFor(model => model.Fighter2Login) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FightStatusID) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FightStatusID) %>
        <%: Html.ValidationMessageFor(model => model.FightStatusID) %>
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<% } %>

这是我战斗的ViewModel:

public class FightDetailsViewModel
{
    public Fight Fight { get; set; }
    public List<Fighter> Fighters { get; set; }
}

有没有视图模型战机(即没有参与这种情况下,反正)。

There is no ViewModel for Fighters (none that is involved in this scenario, anyway).

我可以张贴任何code,你不妨去看看。

I can post any code you may wish to see.

编辑:我看了<一个href=\"http://stackoverflow.com/questions/2160171/using-viewmodel-pattern-with-mvc-2-strongly-typed-html-helpers\">http://stackoverflow.com/questions/2160171/using-viewmodel-pattern-with-mvc-2-strongly-typed-html-helpers和<一个href=\"http://stackoverflow.com/questions/2936927/asp-net-mvc-2-updatemodel-is-not-updating-values-in-memory-or-database\">http://stackoverflow.com/questions/2936927/asp-net-mvc-2-updatemodel-is-not-updating-values-in-memory-or-database ,但我还没有看到解决办法还没有。

I've looked at http://stackoverflow.com/questions/2160171/using-viewmodel-pattern-with-mvc-2-strongly-typed-html-helpers and http://stackoverflow.com/questions/2936927/asp-net-mvc-2-updatemodel-is-not-updating-values-in-memory-or-database , but i haven't seen a solution there yet.

推荐答案

取而代之的是的UpdateModel(战斗机战斗机); 尝试调用updte模型就这样的UpdateModel(战斗机); 。所不同的是两者之间的编辑,在战斗机的情况下,你的模型是直接战斗机,所以你并不需要的名字,而在战斗的情况下,你调用编辑器model.Fight所以你需要的名字。看到这个问题还有:<一href=\"http://stackoverflow.com/questions/3147739/asp-net-mvc2-how-to-get-model-and-model-something-in-the-same-way-in-controller/3148156#3148156\">http://stackoverflow.com/questions/3147739/asp-net-mvc2-how-to-get-model-and-model-something-in-the-same-way-in-controller/3148156#3148156

Instead of this UpdateModel(fighter, "Fighter"); try calling the updte model just like this UpdateModel(fighter);. The difference is between the two edits that in case of Fighter your model is directly the Fighter so you do not need the name, while in case of the Fight you call the Editor for model.Fight so you need the name. See this question as well: http://stackoverflow.com/questions/3147739/asp-net-mvc2-how-to-get-model-and-model-something-in-the-same-way-in-controller/3148156#3148156

这篇关于在MVC中,一个编辑HttpPost工作时,另一个不是。我在想什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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