Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
[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

C#
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...
Posted
Updated 19-Nov-14 18:55pm
v2

The idea is simple. Before saving check if the data already exists and tell the user that such and such already exists.
 
Share this answer
 
Comments
Jineesh TR 20-Nov-14 4:35am    
How can I check that data already exists?
Sinisa Hajnal 20-Nov-14 6:30am    
Search in your data source (database?) if entered data already exists (only those fields that uniquely define the user). If you find it, then that user is already entered.
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.
 
Share this answer
 
v2
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.
 
Share this answer
 
As others suggested the normally this requirement is fullfilled by redirecting to some other view,i.e success page.
Otherwise in general when you add data then you would also need to validate data for ex :
if the username already exists,or email already exist (according to your requiremtn) then you would validate this and check this on server side,this would also resolve your issue.
 
Share this answer
 
change "return View()" for "return RedirectToAction()"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900