Sitecore MVC vs. ASP.net MVC
In ASP.net MVC a request always correspond to a MVC route. This has one controller which is being instantiated and one action which is being invoked. In Sitecore MVC a request correspond to a Sitecore item (usually) where the layout is defined in the presentation details of the item. Focusing on Sitecore Controller Renderings, this means that we can have multiple controllers during one request, where multiple actions are invoked.
Multiple forms
You can always have multiple forms on a page (e.g. a login form in the middle of the content and a search box in the top right corner). In ASP.net MVC each form is posted to a specific controller/action. In Sitecore MVC you usually post to the same url (as you want to return the same item again). The result of this is, that each [HttpPost]-action on every of your controller is executed (and not only the one for the form you have submitted).
There are several solutions available to solve this problem:
For the first two we need to consider what to do with validation on the server side. I like the third option the most, as it's very close to traditional ASP.net MVC.
MVC Forms localization
When you don't use Web Forms for Marketers for building your forms, you need to translate the input field labels and the validation messages somehow programmatically. Usually in ASP.net this can be done with resource files, but in Sitecore we want to use Sitecore Dictionaries for this. There are two things you need to implement for making this possible:
MVC Areas
The third piece is about organizing your code. When you have multiple sites with different layouts in one Sitecore installation (e.g. a corporate site and closed user group) then you can split all the controllers, models and views into separate areas. Sitecore has no support for areas out of the box, but there are multiple solutions available for integrate them in your solutions:
We also saw that Sitecore is working on a solution to integrate areas into the core. Rumor has it that they will integrate a similar pattern as BrainJocks does with the mvc.renderRendering pipeline.
In ASP.net MVC a request always correspond to a MVC route. This has one controller which is being instantiated and one action which is being invoked. In Sitecore MVC a request correspond to a Sitecore item (usually) where the layout is defined in the presentation details of the item. Focusing on Sitecore Controller Renderings, this means that we can have multiple controllers during one request, where multiple actions are invoked.
Multiple forms
You can always have multiple forms on a page (e.g. a login form in the middle of the content and a search box in the top right corner). In ASP.net MVC each form is posted to a specific controller/action. In Sitecore MVC you usually post to the same url (as you want to return the same item again). The result of this is, that each [HttpPost]-action on every of your controller is executed (and not only the one for the form you have submitted).
There are several solutions available to solve this problem:
- Post the form via Ajax to a specific controller of your choice
- Use the Sitecore Form handler to post your form to a specific controller
- Use a custom ÀctionMethodSelector
For the first two we need to consider what to do with validation on the server side. I like the third option the most, as it's very close to traditional ASP.net MVC.
MVC Forms localization
When you don't use Web Forms for Marketers for building your forms, you need to translate the input field labels and the validation messages somehow programmatically. Usually in ASP.net this can be done with resource files, but in Sitecore we want to use Sitecore Dictionaries for this. There are two things you need to implement for making this possible:
- Write a custom MetaDataProvider to translate the labels
- Write a DataAnnotationsModelValidator for each available validator
MVC Areas
The third piece is about organizing your code. When you have multiple sites with different layouts in one Sitecore installation (e.g. a corporate site and closed user group) then you can split all the controllers, models and views into separate areas. Sitecore has no support for areas out of the box, but there are multiple solutions available for integrate them in your solutions:
- Resolve the area in the mvc.renderRendering-pipeline
- Use a custom ControllerRunner and a custom Renderer
- Resolve the area by the <site> configurations
We also saw that Sitecore is working on a solution to integrate areas into the core. Rumor has it that they will integrate a similar pattern as BrainJocks does with the mvc.renderRendering pipeline.