如何$形式的对$ pvent补办又回到了它的时候? [英] How to prevent re-submit of form when getting back to it?

查看:162
本文介绍了如何$形式的对$ pvent补办又回到了它的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些常规输入网页,用户对项目类型中的一些细节。
然后,用户presses提交按钮后,她被重定向到一个网页说,一切正常。的问题是,如果她点击后退按钮然后输入页面再次显示,如果她击中填满,表单被重新提交和另一个项目被写入到数据库中,这是不希望的,当然。

I have some regular input webpage where users type in some details about item. Then after the user presses the submit button she is redirected to a webpage saying that everything went fine. The problem is that if she clicks the back button then the input page is displayed again, and if she hits the sumbit, the form is re-submitted and another item is being written to the database, which is not desirable, of course.

我在C#中使用ASP.NET MVC。什么是最好的实践方式来处理这种情况呢?

I'm using ASP.NET MVC in C#. What is the best-practice way to deal with this situation?

推荐答案

您可以使用TempDate和隐藏的表单值都存储一个独特的标记,并在回传他们进行比较。

You can use TempDate and a hidden form value to both store a unique token, and compare them on the postback.

这样的:

public ActionResult FormDemo()
{
    var guid = Guid.NewGuid().ToString();
    ViewData.Model = guid;
    TempData.Add("guid", guid);
    return View();
}

[HttpPost]
public ActionResult FormDemo(string name, string guid)
{
    if(guid != (string)TempData["guid"]) return new HttpStatusCodeResult(500);
    return RedirectToAction("FormDemoReceipt");
}

public ActionResult FormDemoReceipt()
{
    return Content("Form submitted OK!");
}

我FormDemo看法是这样的:

My FormDemo view looks like this:

@using (Html.BeginForm())
{
    @Html.Hidden("guid", Model)
    @Html.TextBox("name");
    <input type="submit"/>
}

这篇关于如何$形式的对$ pvent补办又回到了它的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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