0

我有以下视图:

    @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?

提前致谢...

4

1 回答 1

0

找到了。我无意中将第二种方法设为私有。相当愚蠢的错误...

于 2013-07-09T11:12:54.617 回答