在this question禁用提交按鈕以防止多次回發是停止(非惡意)用戶多次回發的可行解決方案。在MVC中防止多次提交
如果您有多個提交按鈕的視圖,則此選項無法正常工作。以下面的例子。
//查看:
@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中的這個問題。有人知道如何將禁用控件的值傳遞給服務器嗎?
我可能不得不爲這個看似簡單的問題開始賞金...... – Coda