在 MVC 中,一个 Edit HttpPost 正在工作,另一个则不工作。我缺少什么?

StackOverflow https://stackoverflow.com/questions/3400949

  •  25-09-2019
  •  | 
  •  

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

我正在从头开始构建我的第一个 MVC 应用程序,按照 MVC 音乐商店示例 而是构建一个小应用程序,可以在其中创建竞技场战士并让它们互相战斗。(FightersFight 已通过 EF 链接到基础表)。

我有两个控制器 FightersFights. 。编辑 Actionresult 为了 Fights 正在工作,但是对于 Fighters 它不是。当我点击按钮保存更改时,我返回到关联的索引页面,但尚未提交任何更改。这是我的问题:为什么会失败?

兵营控制器, ,带有错误的非更新 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);
            }

        }

(如你所见,我已经包括了 这个问题中的 GetModelStateErrors 技巧, ,但 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>

它在共享中使用以下 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; }
}

战斗机没有 ViewModel(无论如何,这个场景中都没有涉及)。

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

编辑:我看过 将 ViewModel 模式与 MVC 2 强类型 HTML 帮助程序结合使用ASP.NET MVC 2 UpdateModel() 不更新内存或数据库中的值 ,但我还没有看到解决方案。

有帮助吗?

解决方案

而不是这个 UpdateModel(fighter, "Fighter"); 尝试像这样调用 updte 模型 UpdateModel(fighter);. 。两种编辑之间的区别在于,如果是战斗机,您的模型直接是战斗机,因此您不需要名称,而如果是战斗,您可以调用模型的编辑器。战斗,因此您需要名称。另请参阅这个问题: asp.net mvc2 - 如何在控制器中以相同的方式获取 model 和 model.something ?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top