在这个问题中,禁用提交按钮以防止多次回发是阻止(非恶意)用户多次回发的可行解决方案。
如果您有一个包含多个提交按钮的视图,则此选项效果不佳。举个例子。
//看法:
@using (Html.BeginForm("Catalog", "Images", FormMethod.Post, new { onsubmit = "Utility.disable_buttons(this.id);", id = "catalog_form" }))
{
<p>
<input type="submit" name="btnSubmit" value="Clear" /> |
<input type="submit" name="btnSubmit" value="Catalog" /> |
<input type="submit" name="btnSubmit" value="Update" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
//控制器:
[HttpPost]
public ActionResult Catalog(string btnSubmit)
{
switch (btnSubmit)
{
case "Catalog":
//More differenter code
break;
case "Clear":
//Different code
break;
case "Nothing":
//Code
break;
}
return View();
}
在视图中,存在三种不同的提交操作。如果按钮被禁用,那么它们的值将不会被传递,控制器将不知道哪个按钮触发了提交。(控制器总是为空。)不幸的是,submitdisabledcontrols 属性似乎不能解决 MVC 中的这个问题。有人知道如何将禁用控件的值传递给服务器吗?