如何在MVC 4中检查页面刷新(F5)? [英] How do I check for a page refresh (F5) in MVC 4?

查看:156
本文介绍了如何在MVC 4中检查页面刷新(F5)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

我需要你的帮助..
我坚持使用此代码..
我有一张注册表.当我按提交时,数据已提交并且可以正常工作.
但是,当我按F5键时,就会出现问题.现在,数据再次提交.因此重复.我该如何检查..
我使用了动作过滤器,在其中保存了路线数据.但是仍然无法正常工作..

我的控制器

Hi all..

I need a help from you..
I have stuck with this code..
I have a registration form. When I press submit, data is submitted and it works fine.
But the problem arises when I press F5. Now the data again submitted. and thus duplicated. How can I check for this..
I have used Action filter, in which I have saved the route data. But still it is not working..

My Controller

[HttpPost]
        [RefreshDetectFilter]
        public ActionResult Register(DownloadForm download, string downloadSave)
        {
                download.enquiryLabelID = 708;
                download.enquiryTypeID = 41;
                if ((bool)RouteData.Values["IsRefreshed"] == false)
                {
                    int Id =  objSMWebDefaultConnection.smConn.spUNCandidateTestRegister(download.unCandidateFirstName, download.unCandMobilePhone, download.unCandEmail, download.enquiryTypeID, download.enquiryLabelID).ToList().ElementAt(0).getInteger();
                    objSMWebDefaultConnection.smConn.SaveChanges();
                    ModelState.Clear();
                    download = new DownloadForm();
                    download.candidateID = Id;
                }
                else
                {
return View("~/Views/Download/Index.cshtml", download);
                }
        }



动作过滤器



Action Filter

public class RefreshDetectFilter : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var cookie = filterContext.HttpContext.Request.Cookies["FindRefreshFilter"];

            filterContext.RouteData.Values["IsRefreshed"] = cookie != null &&

            cookie.Value == filterContext.HttpContext.Request.Url.ToString();
        }
}




任何人都有一个主意吗?

请帮助我...




Any one have an Idea???

Please help me...

推荐答案

这个想法很简单.在保存之前,请检查数据是否已经存在,并告诉用户该数据已经存在.
The idea is simple. Before saving check if the data already exists and tell the user that such and such already exists.


请尝试以下 LINK [ filterContext.RouteData.Values ["IsRefreshed"] = cookie!= null&&
cookie.Value == filterContext.HttpContext.Request.Url.ToString();
}
公共无效OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.SetCookie(new HttpCookie("RefreshFilter",filterContext.HttpContext.Request.Url.ToString()));
}
}
在global.asax中注册.然后,您可以在您的控制器中执行此操作:

如果(RouteData.Values ["IsRefreshed"] == true)
{
//页面已刷新.
}
您可能希望改进检测以检查所用的HTTP方法(因为POST和GET URL看起来相同).请注意,它使用cookie进行检测.
Try this LINK[^]

and this link also LINK[^]


You can use a ActionFilter that looks something like this:

public class RefreshDetectFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var cookie = filterContext.HttpContext.Request.Cookies["RefreshFilter"];
filterContext.RouteData.Values["IsRefreshed"] = cookie != null &&
cookie.Value == filterContext.HttpContext.Request.Url.ToString();
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.SetCookie(new HttpCookie("RefreshFilter", filterContext.HttpContext.Request.Url.ToString()));
}
}
Register it in global.asax. Then you can just do this in your controller:

if (RouteData.Values["IsRefreshed"] == true)
{
// page has been refreshed.
}
You might want to improve the detection to also check the HTTP Method used (as a POST and GET url can look the same). Do note that it uses a cookie for the detection.


解决这些问题的正常方法是在提交表单后将其重定向到另一个page \ view \ action,因此您可以将其接收处理完表单后转到成功"页面.
The normal solution for these problems is to redirect to a different page\view\action after the form has submitted, so you can take them to a "Success" page after processing the form.


这篇关于如何在MVC 4中检查页面刷新(F5)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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