I’m trying to understand what the equivalent of a user control is within an MVC application? From what I understand, it’s a Partial. The problem I’m finding is that when I use a user control, I’m able to encapsulate the logic within that control and re-use it across multiple pages without having to worry about the parent page.
How does this work with MVC? There doesn’t seem to be any way to encapsulate logic with a partial view.. in fact it’s confusing the hell out of me.
Does this mean that for any page that might use the partial, the controller would always have to return the data for the partial just in case it’s required? Doesn’t this seem incredibly inefficient? I can’t seem to wrap my head around how this works…
You can add logic using razor syntax. For example, you can use conditional statements to manipulate the final output.
In addition to this you can use @helper syntax like
The logic in partials is slightly different to what you are used to in Web Forms user controls. The above logic can be placed in a Partial so that you can reuse it later, you just need to place it in /Views/Shared/DisplayTemplates or EditorTemplates.