Forms MVC4中的FormMethod.Post具有意外行为
我有以下看法:Forms MVC4中的FormMethod.Post具有意外行为,forms,asp.net-mvc-4,Forms,Asp.net Mvc 4,我有以下看法: @model DocuLive.ViewModels.InstallationRequestViewModel @{ ViewBag.Title = "AttachDB"; Layout = "~/Views/Shared/_AdminPage.cshtml"; } <h2>AttachDB</h2> @using (Html.BeginForm("AttachDB","AppSt
@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>
}
@model DocuLive.ViewModels.InstallationRequestViewModel
@{
ViewBag.Title=“AttachDB”;
Layout=“~/Views/Shared/_AdminPage.cshtml”;
}
附件
@使用(Html.BeginForm(“AttachDB”、“AppStart”、FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
您尝试安装的服务器上已存在数据库DocuLive。是否要将现有数据库附加到此DocuLive安装?下面的详细信息将用于此尝试
数据库服务器凭据
@DisplayNameFor(model=>model.Server)
@DisplayFor(model=>model.Server)
@DisplayNameFor(model=>model.UserName)
@DisplayFor(model=>model.UserName)
@DisplayNameFor(model=>model.Password)
@DisplayFor(model=>model.Password)
}
我在控制器中有以下两种方法:
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");
}
}
公共操作结果AttachDB(InstallationRequestViewModel requestVM)
{
if(requestVM!=null)
返回视图(“AttachDB”,requestVM);
其他的
{
TempData[“Fail”]=“DocuLive数据库存在,但Incalization请求的值为空,无法用于附加数据库”;
返回操作(“索引”);
}
}
[HttpPost]
专用异步任务AttachDB(InstallationRequestViewModel requestVM,string命令)
{
尝试
{
开关(命令)
{
案例“附件”:
//做一些复杂的事情,然后返回到Action
案例“重新开始”:
返回操作(“索引”);
违约:
返回操作(“索引”);
}
}
捕获(例外情况除外)
{
TempData[“Fail”]=例如消息;
返回操作(“索引”);
}
}
出于某种原因,当我使用任意一个按钮提交表单时,它会点击第一个方法,而不考虑我明确指定了FormMethod。发布表单以确保提交表单将使用户转到第二个方法,该方法实际上包含一些业务逻辑
这很奇怪,因为我在整个应用程序中都使用了类似的方法,到目前为止我对此没有任何问题
有谁能告诉我,为什么用任何一个按钮提交表单都被认为是Get而不是POST
提前谢谢…找到了。我无意中把第二种方法保密了。相当愚蠢的错误