【问题标题】:In MVC, one Edit HttpPost is working, the other one isn't. What am I missing?在 MVC 中,一个 Edit HttpPost 工作正常,另一个不工作。我错过了什么?
【发布时间】:2011-03-24 23:27:28
【问题描述】:

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

我正在从头开始构建我的第一个 MVC 应用程序,使用 MVC Music Store example,而是构建一个小应用程序,可以在其中创建竞技场战士并让他们互相战斗。 (FightersFight 已通过 EF 链接到基础表)。

我有FightersFights 的控制器。 Fights 的编辑 Actionresult 有效,但 Fighters 无效。当我点击按钮保存我的更改时,我返回相关的索引页面,但没有提交任何更改。这是我的问题:为什么会失败?

来自 BarracksController,带有错误的非更新 HttpPost Edit(应该被命名为 FighterController,但没关系):

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

        }

(如您所见,我已包含 the GetModelStateErrors trick from this SO question,但 x 的结果是 null

这是可以工作的控制器,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>

在 Shared 中使用以下 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>
<% } %>

这是我的战斗视图模型:

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

没有用于 Fighters 的 ViewModel(无论如何,在这种情况下都没有涉及)。

我可以发布您可能希望看到的任何代码。

编辑:我看过 Using ViewModel Pattern with MVC 2 Strongly Typed HTML HelpersASP.NET MVC 2 UpdateModel() is not updating values in memory or database ,但我还没有看到解决方案。

【问题讨论】:

  • 我假设 EditorForModel 调用模型的 ascx 页面。在这种情况下,我还注意到您有两个 BeginForm() 电话 - 只是想知道这是否可能是问题
  • 好消息:我太草率了。但是:我编辑了适当的edit.aspx(更新了问题以反映更改)并且问题/未更新仍然存在。
  • 我刚刚注意到...我的 Fighters 的 edit.aspx 有 Html.EditorForModel()。那应该是Html.EditorForModel("Fighter")吗? (我假设没有区别,但假设是....)我不在 PC 后面,我可以测试它现在是否有效。
  • EditorForModel("Fighter") 将使用模板,而之前的调用将为您生成输入字符串。在这种特殊情况下,这就是您要使用的模板,对吧?

标签: asp.net-mvc asp.net-mvc-3


【解决方案1】:

代替UpdateModel(fighter, "Fighter"); 尝试像UpdateModel(fighter); 那样调用updte 模型。两种编辑之间的区别在于,在 Fighter 的情况下,您的模型直接是 Fighter,因此您不需要名称,而在 Fight 的情况下,您调用 model.Fight 的编辑器,因此您需要名称。也可以看看这个问题:asp.net mvc2 - how to get model and model.something in the same way in controller?

【讨论】:

  • 我相信我也尝试过这种排列方式,但稍后当我使用合适的 PC 时,我会确定。谢谢。不过有点奇怪,更具声明性的语法不应该在简单语法的地方工作(如果你的答案是正确的)?
  • 这不是随机尝试排列。在一种情况下,它只有在您在另一种情况下定义前缀时才会起作用,只有在您不这样做时才会起作用。这取决于您是要绑定对象(不需要前缀)还是 object.something(需要前缀)。所以,如果你能解决这个问题,请告诉我......
  • 我不是随机尝试排列(我可能是新人,但不是那么新)。只是 UpdateModel 调用(1 参数或 2)的区别是(是),在我看来,0。这就像第二个参数是一个规范,只有在您不使用默认值时才需要,但指定默认值("Fighter",在我的情况下)应该与不指定它相同。但是通过阅读您,我实际上正在寻找Fighter.Fighter,就像我现在正在做的那样,这是错误的。当我在正确的机器后面时(应该在几个小时内),我会尽快回复你。
猜你喜欢
  • 2020-03-25
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多