3

这个问题中,禁用提交按钮以防止多次回发是阻止(非恶意)用户多次回发的可行解决方案。

如果您有一个包含多个提交按钮的视图,则此选项效果不佳。举个例子。

//看法:

@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 中的这个问题。有人知道如何将禁用控件的值传递给服务器吗?

4

2 回答 2

2

一些背景资料:

如果您想禁用所有按钮,使它们不再可点击,但仍想知道用户点击了什么,我能想到的唯一解决方案是隐藏字段以及此代码:

$(function() {
    $('input[type=submit]').click(function() {
        var form = $(this).closest('form');
        form.find('#hiddenField').val($(this).val());
        form.find('input[type=submit]').prop('disabled', true);
    });
});

如果您只想确保用户在单击一个按钮后无法单击其他按钮,您可以这样做:

$(function() {
    $('input[type=submit]').click(function(event) {
        $(this).closest('form').find('input[type=submit]').not(this).prop('disabled', true);
    });
});

不幸的是,没有解决方案(我知道)将处理程序绑定到表单的提交并确定单击了哪个输入。这不是与您的问题相关的问题,但会使上面的代码稍微复杂一些。

于 2012-01-07T14:23:52.543 回答
0

你可以试试这个

if (coll.AllKeys.Contains("Clear"))
{
  // your code here for this submit
}
于 2011-08-29T08:39:18.527 回答