防止 MVC 中的多次提交

发布于 2024-12-02 03:48:06 字数 1193 浏览 2 评论 0原文

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

如果您的视图具有多个提交按钮,则此选项效果不佳。以下面的例子为例。

//View:

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

//Controller:

[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();
    }

在视图中,有三种不同的提交操作。如果按钮被禁用,那么它们的值将不会被传递,控制器将不知道哪个按钮触发了提交。 (控制器总是获取 null。)不幸的是,submitdisabledcontrols 属性似乎并没有解决 MVC 中的这个问题。有人知道如何将禁用控件的值传递给服务器吗?

In this question disabling the submit button to prevent multiple postbacks was a viable solution to stop (non-malicious) users from posting back multiple times.

This option doesn't work well if you have a view with multiple submit buttons. Take the following example.

//View:

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

//Controller:

[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();
    }

In the view, there are three different submit actions. If the buttons are disabled, then their values won't be passed and the controller will not know which button triggered the submit. (The controller always gets null.) Unfortunately, the submitdisabledcontrols attribute does not seem to solve this problem in MVC. Does anybody know how to pass disabled control's values to the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-12-09 03:48:06

一些背景信息:

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

$(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);
    });
});

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

Some background info:

If you want to disable all buttons so they are no longer clickable but still want to know what the user clicked the only solution I can think of is a hidden field along with this code:

$(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);
    });
});

If you just want to make sure the user cannot click the other buttons after clicking one you can do this:

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

Unfortunately there is no solution (I know of) to bind a handler to the form´s submit and determine which input was clicked. This is not a problem related to your question but makes the above code slightly more complicated.

荒岛晴空 2024-12-09 03:48:06

你可以试试这个

if (coll.AllKeys.Contains("Clear"))
{
  // your code here for this submit
}

you can try this

if (coll.AllKeys.Contains("Clear"))
{
  // your code here for this submit
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文