3

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

5
  • I've used the Session property to do something like this before. See: code.msdn.microsoft.com/How-to-create-and-access-447ada98 or stackoverflow.com/questions/20350327/… I'm not sure if it works across different controllers, but it worked like a charm for me for sharing data between methods of the same controller. Commented Nov 20, 2015 at 23:55
  • You can use TempData or Session or even save a partial transaction in a database table. Commented Nov 20, 2015 at 23:59
  • You could serialise to JSON & store it in a hidden field on each subsequent form. You wouldn't need to use session state then, Commented Nov 21, 2015 at 1:48
  • Why not just use cascading dropdownlists (using ajax to populate the 2nd one based on the value of the first) so you can do all this in one form without unnecessary redirects and the need to persist the data? Commented Nov 21, 2015 at 2:09
  • I'll look to give some of these a try (I'm not in the office today) when I get back in front of it. The only comment I can specifically answer is to the drop down thing: To which my reply would be that I want to create a list of buttons they can click that will fire one view to the next, and also because this is the specifications for the design Commented Nov 23, 2015 at 18:02

1 Answer 1

1

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.

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

2 Comments

If I understand this concept correctly, you are suggesting that I make a different ViewModel for each step, then have a Master ViewModel that ropes them all together as they're used so that at the end of the process I have a collection of ViewModels to access all the data from?
@AlexWatts: That's correct. This helps your to avoid multiple calls to your controller and your main concern about persistence will not be a concern if you follow this method.

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.