Jesse
Jesse

Reputation: 1225

ASP.NET MVC Using actions as partial views

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?

Upvotes: 0

Views: 459

Answers (1)

mao47
mao47

Reputation: 967

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.

Upvotes: 3

Related Questions