3
votes

I am developing multi step form with sitecore and mvc. In each step I need the sitecore context and I am using controller rendering for the same. Now as sitecore works I need to add the controller name and action for my rendering so basically I can only use one controller action for each rendering.

In my case I have used the same action name but different parameters type for each step in multi step form. Ideally I would like to have different controller action name for each step within multi step form. Can anyone please help me with that?

Thanks

2
Hi, I don't know Site Core more than it's a CMS but if I'd have a similar problem in another CMS I think I would have written some MVC routing rules. Now you can say that this {prefix}/{controller}/{action} goes to this specific controller/action. Could that be a solution?Andreas
Hi Andreas. Thanks for your response but this many not work. As I mentioned in my post I also need the Sitecore context available in my action.Tech Learner

2 Answers

1
votes

I'm not sure I understand the question completely. Are you trying to use a single .cshtml file for all the steps? If so, you can define multiple controller renderings for the same View in the Sitecore CMS. In the renderings section, define each step of your multi-step form as separate controller renderings. You can use the same controller for each, but specify a different action.

In your controller logic, you can specify to return the same View, but pass different properties into your Model object.

For example, you might do something like this:

public ActionResult Step2(){
   var context = RenderingContext.Current.PageContext.Item;
   var otherParams = "SomethingForStep2";
   var model = new MyModelObject(context, otherParams);
   var view = this.View("Path/To/My/View", model);

   return view;
}

In the example above, I assume you have defined some sort of Model object where you can pass in whatever parameters you need so that your view can use this to render.

If you are using different views, then you will just return a different view for each action, again passing in a model to the View to help it render.

0
votes

If you are using AJAX (which I would assume) I would recommend the following:

  1. Serve each view/step of your multi-step form using a unique HttpGet action in your controller
  2. Represent each view/step of the form with a unique item and a unique view (assign the views to the items and use an empty Layout to enable the form/view to be rendered on its own)
  3. Handle each form's post action with a unique HttpPost action in your controller
  4. From the HttpPost action, return a JSON object which can be handled client-side. Include in this result properties such as success, failure, validation errors and the next step of the form to display (a URL for the following AJAX request - call this property NextStepUrl)
  5. In your client-side JavaScript have a function to handle the JSON result object returned by your controller's POST action. If successful read the NextStepUrl property then use this to perform an AJAX GET request for the next step of the form

This approach means that on each GET request you will have a valid RenderingContext from which to read your controller rendering's datasource.

If you need to have the RenderingContext during your HttpPost actions you can simply include a hidden field with the ID of the datasource item and read this in the HttpPost action, or you can use some custom model binding to achieve the same thing in a slightly cleaner way.