I'm using ASP.NET MVC to separate my HTML views from my Models. However there's a specific situation that is confusing me slightly.
I have several commonly used small panels of information, which themselves are comprised of a number of smaller panels of information. these get their data from a subclass contained within the various models, sometimes single instances, sometimes lists of objects.
At present this is done using Partial views, passing the appropriate piece of data via the model parameter, as such:
@Html.Partial("UserInfo", this.Model.CurrentUser);
@Html.Partial("UserInfo", reply.PostedBy);
And so on. This all works fine.
I've recently hit a requirement which feels like it stretches sensible limits for this model however - it will involve a very large number of partial views with a TINY amount of HTML in each, nested numerous times. The page resolution time seems to be starting to get a little out of hand, and I suspect the amount of searching for and reflecting partial views might have something to do with it.
Please note: I'm assuming duplicating HTML which is supposed to be the same is still to be avoided. I could simplify the nest by having copies of HTML in some of the higher level controls, but it strikes me this damages maintainability.
For the inner most ones it would seem to make more sense to me to create static helper classes which generate and return the required HTML - however, despite the fact MVC itself does it with the Html
helper class, it feels like this goes against the MVC pattern.
- Is it OK to use static helper classes to generate small HTML snippets?
- Where should the static
UserInfo
class go? Views? Controllers? Elsewhere?
Obviously this approach still separates the helper method from the model, but as it takes a model in to work with, I don't really see how uncoupled it really is.
A static helper is a hairs breadth away from an extension method, consumable in a userInstance.InfoHtml()
type of way, which just seems to make the whole approach really similar to just adding the helper method to the model. This is of course what MVC is trying to get away from in the first place!
Please note: I'm not trying to sneak around the rules or complain! I just want to approach this as "on pattern" as possible. If lots and lots of partial views is the way, I'll stick with that and performance tune as best I can.