检测浏览器刷新与表单提交在ASP.Net MVC 2 [英] Detect Browser Refresh vs. Form Submit in ASP.Net MVC 2

查看:187
本文介绍了检测浏览器刷新与表单提交在ASP.Net MVC 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重新提交数据到同一页面,呈现出不同的问题,每次一个ASP.Net应用问卷调查。有一步和下一步按钮的问题之间进行导航。

I have an ASP.Net questionnaire application that resubmits data to the same page, showing a different question each time. There are BACK and NEXT buttons to navigate between questions.

我想,当窗体是由于浏览器刷新主场迎战按钮是pressed之一提交的检测。我碰到一个 WebForms的方法但别来了知道如何应用这些校长在MVC 2的应用,因为页面的事件不可用(据我知道......我是pretty新微软的MVC模式)。

I would like to detect when the form is submitted due to a browser refresh vs. one of the buttons being pressed. I came across a WebForms approach but don't know how to apply those principals in an MVC 2 application since page events aren't available (as far as I know... I'm pretty new to Microsoft's MVC model).

如何将一个原则上适用于MVC 2?有没有更好的方法来检测刷新?

How would one apply that principle to MVC 2? Is there a better way to detect refresh?

推荐答案

您可以使用重定向消息后后图案<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.tempdata.aspx\"><$c$c>TempData.例如:

You could use the redirect-after-post pattern with TempData. Example:


  1. 上一步和下一步按钮,一个表单POST给一个控制器动作

  2. 控制器动作使一些国家到的TempData 并重定向到另一个控制器动作,这将验证该数据在的TempData 并返回查看

  3. 用户presses F5在浏览器中,previous行动呼吁GET和作为国家不再为的TempData 您知道用户pressed F5,并没有通过表单提交。

  1. The Back and Next buttons POST a form to a controller action
  2. The controller action puts some state into the TempData and redirects to another controller action which will verify that the data is in TempData and return the view
  3. The user presses F5 on the browser, the previous action is called on GET and as the state is no longer into TempData you know the user pressed F5 and didn't pass through the form submission.

和来说明这一点:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var state = TempData["state"];
        if (state == null) 
        {
            // the user directly arrived on this action without passing 
            // through the form submission
        }
        return View();
    }

    [HttpPost]
    public ActionResult Index(string back)
    {
        TempData["state"] = new object();
        return RedirectToAction("Index");
    }
}

这篇关于检测浏览器刷新与表单提交在ASP.Net MVC 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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