MVC4中的FormMethod.Post具有意外行为

时间:2013-07-09 10:09:50

标签: forms asp.net-mvc-4

我有以下观点:

    @model DocuLive.ViewModels.InstallationRequestViewModel

    @{
        ViewBag.Title = "AttachDB";
        Layout = "~/Views/Shared/_AdminPage.cshtml";
    }

    <h2>AttachDB</h2>
    @using (Html.BeginForm("AttachDB","AppStart", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

        <p>Database DocuLive already exists on the server where you attempted installation. Do wish to attach existing DB to this installation of DocuLive? Details below will be used for this attempt.</p>

        <fieldset>
            <p>
                <input type="submit" name="command" value="Attach" />
                <input type="submit" name="command" value="Start over" />
            </p>
            <legend>DB server credentials</legend>

            <div class="display-label">
                 @Html.DisplayNameFor(model => model.Server)
            </div>
            <div class="display-field">
                @Html.DisplayFor(model => model.Server)
            </div>

            <div class="display-label">
                 @Html.DisplayNameFor(model => model.UserName)
            </div>
            <div class="display-field">
                @Html.DisplayFor(model => model.UserName)
            </div>

            <div class="display-label">
                 @Html.DisplayNameFor(model => model.Password)
            </div>
            <div class="display-field">
                @Html.DisplayFor(model => model.Password)
            </div>
        </fieldset>
    }

我在控制器中有两个方法:

        public ActionResult AttachDB(InstallationRequestViewModel requestVM)
        {
            if (requestVM != null)
                return View("AttachDB", requestVM);
            else
            {
                TempData["Fail"] = "DocuLive database exist, but inicalization request has null value and cannot be used to attach DB";
                return RedirectToAction("Index");
            }
        }


        [HttpPost]
        private async Task<ActionResult> AttachDB(InstallationRequestViewModel requestVM, string command)
        {
            try
            {
                switch (command)
                    {
                        case "Attach":
                            // do something complex and return RedirectToAction
                        case "Start over":
                            return RedirectToAction("Index");
                        default:
                            return RedirectToAction("Index");
                    }
            }
            catch (Exception ex)
            {
                TempData["Fail"] = ex.Message;
                return RedirectToAction("Index");
            }
        }

出于某种原因,当我使用任一按钮提交表单时,它会点击第一个方法而不考虑我为表单明确指定FormMethod.Post的事实,以确保提交表单会将用户带到第二个方法实际上包含一些业务逻辑。

这很奇怪,因为我在应用程序中使用了类似的方法,到目前为止我对此没有任何问题。

有人可以建议,为什么用任何一个按钮提交表单都被认为是Get而不是POST?

提前致谢...

1 个答案:

答案 0 :(得分:0)

找到它。我意外地将第二种方法设为私有。相当愚蠢的错误...