user3210546
user3210546

Reputation: 155

What is the best way to persist data between several views

I am working on an MVC 5 application and have the following scenario. The user clicks my registration link to create an account and is served with a view like so. Works as expected.

[HttpGet]
[AllowAnonymous]
public ActionResult RenderRegisterModal(string userType)
{
    IList<UserTypesDto> userTypes = _userService.GetUserTypes();

    var usersRegisterUserViewModel = new UsersRegisterUserViewModel {UserTypes = new List<SelectListItem>()};

    usersRegisterUserViewModel.UserTypes.Add(new SelectListItem
    {
        Text = "Select a registration type",
        Value = "0"
    });

    foreach (UserTypesDto userTypeDto in userTypes)
    {
        usersRegisterUserViewModel.UserTypes.Add(new SelectListItem
        {
            Text = userTypeDto.UserType,
            Value = userTypeDto.UserTypeId.ToString()
        });
    }

    return PartialView("_RegisterModal", usersRegisterUserViewModel);
}

Now based on the type of customer / userType they choose I need to present different views. Should not have any issue getting the view into the modal as it is ajax... So lets call this a pseudo wizard to collect data. The issue I am having and the basis for this probably simple question, that I am thinking too much about, is how do I save the data from each step?? Temp Table? InMemory Cache using the session id as the key? Cookies? "Gross"

The ActionMethod that the post goes to looks like this.

[AllowAnonymous]
[HttpPost]
public ActionResult Register(UsersRegisterUserViewModel usersRegisterUserViewModel)
{
    //TODO Return a view for the next step based on the CustomerType contained in the viewModel
    return View();
}

Any feedback would be greatly appreciated.

Upvotes: 1

Views: 588

Answers (2)

Romias
Romias

Reputation: 14133

OPTION 1:

The easiest way, is that the NEXT view have a set of hidden fields with the information entered in the previous view and which was posted to the Action.

This way, once you post the second view, you will be posting all the information (previous one and the one you entered in the second view).

OPTION 2:

If you are not comfortable with the first approach, you can have several PartialViews that are shown or hidden in javascript on UserType combo changes. Your ViewModel should have all the propertied you need to hold the information before posting back to server.

This option came in 2 flavors: you render all your usertype partials at the begining (so you need to hide them at first), or you can get the partial via Ajax once the user selected one user type.

OPTION 3:

You can use Session to hold the sensitive data from the register form, redirect to the next view depending on user type, post the new form, and in the Action you retrieve the information from Session... and with all the information in your hands store it in database.

Upvotes: 2

Jakotheshadows
Jakotheshadows

Reputation: 1515

If you're using razor, persist the different parts of your model like this

@Html.EditorFor(model => model.data1)
@Html.EditorFor(model => model.data2) @*Wizard part 1*@

then in the next view for your wizard

@Html.HiddenFor(model => model.data1)
@Html.HiddenFor(model => model.data2)
@Html.EditorFor(model => model.data3)
@Html.EditorFor(model => model.data4) @*Wizard part 2*@

and then

@Html.HiddenFor(model => model.data1)
@Html.HiddenFor(model => model.data2)
@Html.HiddenFor(model => model.data3)
@Html.HiddenFor(model => model.data4)
@Html.EditorFor(model => model.data5) @*Wizard part 3...*@

and so on...

Upvotes: 0

Related Questions