Thursday, February 23, 2017

Don't write logic into your Views, use a Controller

The view contains 4 lines of Markup but 15 lines of code. Don't do that - it's better to use a Controller that contains an Action which contains the logic and returns a model to the view.
Change your Sitecore presentation Item from View rendering to Controller rendering and set the Controller assembly path and action:
The action should contain all logic:
And the view only the markup:
But even the Model.Count == 0 is too much logic. The model could contain a field "HasItems" that checks the model count, but this can be accepted. After refactoring, the view contains more lines of Markup than logic.

No comments:

Post a Comment