0

I have a view that loads several other partial views, passing data from the original view's model to the partial views as needed.

My question is, should I create separate controllers (to represent the different db objects) and use Html.RenderAction() to load the partial views from the original view, or is it okay to just do all the business logic at once, put it into my view model, and load the partial views using Html.Partial()?

If I created seperated controllers and used Html.RenderAction(), those actions would not be accessed anywhere except from the original view (all the actions are partial views), so it seems strange to do that.

I still have other controllers that act as endpoints for the website, but this seems to be a different case where creating more controllers that only have partial view actions seems useless.

Is there some standard practice here, or is it just preference at this point?

1 Answer 1

3

You should NOT use Html.RenderAction(), as this creates a new MVC pipeline which can adversely affect performance. You should usually use Html.Partial() or Html.RenderPartial(). As for creating separate controllers, it really depends on your scenario. If those partial actions should be logically grouped with that controller I would put them there, but if the controller is becoming too monolithic or your partials are perhaps some kind of widget that is displayed on many different areas of your site it may be good to put them in a separate WidgetController.

Summary: use Html partial helpers to avoid an extra MVC pipeline, decide which controller it makes sense to use for the partials in your specific situation.

EDIT: I mis-remembered the difference between Action and Partial methods. Use Partial() and RenderPartial() wherever possible. These do avoid creating another pipeline; the downside being you do need to get all the information returned in the main action and passed through the parent view. Action() and RenderAction() are heavier in terms of performance, but they can be used to call outside controllers and actions, allowing you to separate your logic when necessary.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help. What do you think about creating a controller whose actions ONLY return partial views, and are ONLY called from a single place (that is, a particular view). Surely this means there is no need for that controller, and to just use RenderPartial(), correct?
Yes that would be a situation to just use renderpartial(). I'm afraid I may have mis-remembered the differences between the two methods though, seems RenderPartial and Partial must be supplied with the data immediately, in which case the main controller would be responsible. Updating my answer now.
I would also add, it is advantageous to create partial action/views if you decide to go down the Ajax route. I'd also recommend using the ChildActionOnly Attribute to reduce your security surface (if needed).
Great! I'll have the main controller do all the business logic and simply render the other views as partial views from the original view (instead of partial views from a different action/controller).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.