Search code examples
asp.net-mvcasp.net-mvc-4registration

In ASP MVC, how should I structure my controler for a registration process with confirmation page


I am still new to ASP MVC 4 and I need some help to design my controler for a basic registration scenario with a confirmation page.

I have a form where the user enters some personal information, with some model validation using validation attributes. On the submit of this form, I need to call my datalayer for more complex validations before redirecting to a confirmation that shows to the user's input so that he can verify everything. So it is only on the submit of this confirmation form that the create action happens in the database.

I read about the PRG pattern (Post-redirect-Get) and I am a bit confused because I do not want any of the personal data to appear in the URL between the registration and the confirmation form.

I hope I am clear enough in the explation of my situation..

This is a pretty basic scenario that we see pretty much everywhere so I hoped to find exmaples somewhere but I can't find any article or tutorial about it.

Thanks in advance!


Solution

  • Well I would build the controller class as shown below .. I will be using session to store the user information to get it to the confirmation page, but you can choose any other ways maybe like a cookie if that is okay from a security perspective

    Public class RegistrationController:Controller
    {
        [HttpGet]
        public FillInformation()
        {
            ...
            Return view();
        }
    
        [HttpPost]
        public FillInformation(UserInformation UserInformation)
        {
            if (ModelState.IsValid)
            {
                //do some further validation here and if succeed then save to session
                Session["info"] = UserInformation;
                return RedirectToAction("Confirmation");
            }
    
            Return view(UserInformation);
        }
    
        [HttpGet]
        public Confirmation()
        {
            // get object from session
            UserInformation info = Session["info"]
            Return view(info);
        }
    
        [HttpPost]
        public Confirmation()
        {
            // get object from session
            UserInformation info = Session["info"]
            //Save data to database here
        }
    }
    

    Also since you are using MVC 4, another way to do this would be using a service call by implementing a WEB API service.