prevent双表单提交 [英] Prevent double form submissions

查看:173
本文介绍了prevent双表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 3应用程序,它有一个名为后行动创建

  [HttpPost]
公共虚拟的ActionResult创建(刊号)
{
    如果(ModelState.IsValid)
    {
        context.Issues.Add(问题);
        context.SaveChanges();
        返回RedirectToAction(「指数」);
    }
    返回查看(问题);
}

如果我是双误提交按钮点击,它会创建2个问题。有preventing这种的方式,不使用JavaScript和禁用按钮,我想用整个使用 RedirectToAction 是prevent这通过使用邮政/重定向/获取设计模式?


解决方案

您提到的模式(后/重定向/获取设计模式)prevents页双重张贴刷新,因为最后一个动作是GET,而不是POST

如果您想从双张贴prevent双击,你可以:


  1。禁用后点击按钮
2.保持对'问题'的唯一索引和查找重复的联系人
3.保持会话的东西,给你一个双岗的指示


和可能有几个方面......我想禁用按钮可能是最容易的,因为你要重定向到另一个页面反正。

I have an ASP.NET MVC 3 application which has a post action called Create:

[HttpPost]
public virtual ActionResult Create(Issues issues)
{
    if (ModelState.IsValid)
    {
        context.Issues.Add(issues);
        context.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(issues);
}

If I double click on the submit button by mistake, it'll Create 2x issues. Is there a way of preventing this, without the use of javascript and disabling the button, I thought the whole use of using a RedirectToAction was to prevent this, by using the Post/Redirect/Get design pattern?

解决方案

The pattern you mentioned (Post/Redirect/Get design pattern) prevents page refreshes from double posting because the last action is a GET, not the POST.

If you want to prevent double clicks from double posting, you could:

1. Disable the button after click
2. Maintain a unique index on 'Issues' and look for duplicates
3. Maintain something in session that gives you an indication of a double post

And there maybe a few more ways... I think disabling the button is probably the easiest because you're redirecting to another page anyway.

这篇关于prevent双表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆