3

I'm trying to figure out how to pass a model across views

Here is what I'm doing, I have a Register, RegisterConfirm, RegisterComplete views.

User starts at Register, fills info, clicks continue and posts to RegisterConfirm where they click checkbox to agree to privacy policy then post to RegisterComplete which creates the user based on the model in the first Register view.

Code:

        [GET("Account/Register")]
        public ActionResult Register()
        {
            return View();
        }

        [POST("Account/Register/Confirm")]
        public ActionResult RegisterConfirm(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                return View(model);
            }
            else { return View("Register", model); }
        }

        [POST("Account/Register/Complete")]
        public ActionResult RegisterComplete(RegisterModel model, bool agree)
        {
            if (agree) { 
                // Create User
            }
            return View("Register", model);
        }


View Form

Register:
    @using (Html.BeginForm("RegisterConfirm", "Account", FormMethod.Post, new { @id = "create" }))
    {

Register Confirm:
    @using (Html.BeginForm("RegisterComplete", "Account", FormMethod.Post, new { @id = "create" }))
    {


Problem is, when I'm getting to RegisterComplete, model values are empty... any ideas or is this not possible? Or should this work and I need to double check my registercomplete?

2 Answers 2

4

Is your RegisterConfirm view using display-only elements to show the registration information? If so, MVC won't be able to bind the data to populate the model.

You need to render the model as Input elements, even if they're hidden, so that the model binder can populate RegisterModel (you can render the properties as both hidden elements for 'data retention' and output elements for display).

If you are using Input elements, check that the names of those elements match the property names of RegisterModel, otherwise, again, the model binder won't be able to populate RegisterModel.

If you can't, for whatever reason, put the data in Input elements in your RegisterConfirm view, you'll need to store the data somewhere server-side, either in Session State (or TempData, which uses Session State anyway) or in a database of some description.

The advantage with storing the model server-side is that you can be sure that the data hasn't been tampered with between sending it to the client and receiving it back.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking it was because I didn't have the data elements in the RegisterConfirm, and having hidden input elements wouldnt be viable as RegisterModel will contain their created password, TempData will work though, just tried and passed correctly, so I will use that... Thanks for the advice
4

You can use TempData and store your model inside it and receive your model back from it

[POST("Account/Register/Confirm")]
            public ActionResult RegisterConfirm(RegisterModel model)
            {
                if (ModelState.IsValid)
                {
                    //store data for any other request
                    TempData["newUser"]=model;

                    return View();
                }
                else { return View("Register", model); }
            }

        [POST("Account/Register/Complete")]
        public ActionResult RegisterComplete(RegisterModel model, bool agree)
        {
            //retrieve data back irrespective of use choice 
            //to clear memory

            RegisterModel newUser= TempData["newUser"];

            if (agree) { 
                // Create User
                //use newUser
            }
            return View("Register", model);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.