Alex Watts
Alex Watts

Reputation: 567

Using a ViewModel across multiple controller actions

I'm creating a C# MVC program in which users can rent tractor equipment. The user will choose the type of tractor they want (bulldozer, roller, etc), then be directed to a screen where they can choose a manufacturer who makes that type of tractor (Caterpillar, John Deere, etc). Next, they will be directed to a list of available models of that type of tractor from that manufacturer.

The goal is to have a ViewModel store their input at each step so that I have a collection of their decisions at checkout. If it matters, the only data I'm storing in each step is the specific ID for each choice, and this is being done all in a single Controller using several Actions. At the end, the user will have the option of adding another tractor to their rental ticket, or simply checking out as is. Therefor, I need a way to initialize a new ViewModel object when a new rental form is started, but not create the new ViewModel object if they choose to add more tractors to their current ticket.

My bottom line issue is that I cannot seem to find a way to create a ViewModel in the first step of the rental procedure that will then persist through the entire process, storing data as it goes.

I can post code snippets if it's necessary, but I mostly just need to know how to create a persistent ViewModel object

Upvotes: 3

Views: 859

Answers (1)

Kosala W
Kosala W

Reputation: 2143

You may design it this way. As you have correctly mentioned, in this context, the most important part of your design is your viewModel. So lets combine your OO knowledge with MVC;

   public class MultiStepViewModel
{
    public Step1Data Step1 { get; set; }
    public Step2Data Step2 { get; set; }
    public Step3Data Step3 { get; set; }

    public string StepSummary { get; set; }
}

Step1Data, Step2Data... are individual viewModels. They are part of your master view model which is called MultiStepViewModel

Now you will have to create a main view for "MultiStepViewModel"(Ex: MultiStepView). Then you can create document templates for Step1Data, Step2Data etc.

You can have tabs, menus or any other method to launch your steps. Once all the steps are completed, you can post your MultiStepViewModel. It will have all the data that you filled under each step.

So your controller will be like this.

     public class MultiStepController : Controller
   {
    // GET: MultiStep
    public ActionResult Index()
    {
        return View(new MultiStepViewModel());
    }

    [HttpPost]
    public ActionResult PostMultiStepData(MultiStepViewModel model)
    {
        //your process logic
        if (success)
        {
            return View("It was successfull");
        }
        else
        {
            return RedirectToAction("Index", "MultiStep", model);
        }
    }
}

PS: Make sure to initialise your Step view models inside MultiStepViewModel's constructor. I am not sure what your StepData will look like. If all steps are identical, as you may be aware, you do not need multiple viewMOdels. Instead You can have a IENumerable and create your Document Template accordingly.

Upvotes: 1

Related Questions