I have a partial view called StudentInfo.ascx. In it I have something like this:
<fieldset>
<legend>Students Information</legend>
<div>
<label>First Name:</label>
<label>Last Name:</label>
</div>
</fieldset>
I have another partial view called Detail.ascx which will need to display this StudentInfo.ascx. I did something like this:
<%= Html.RenderAction("StudentInfo"); %>
Is that right?
It depends on what you intend to do and if you use strong type views etc… By the info you provided it would be better to use
RenderPartial()but your partial view doesn’t make any sense the way it is. It lacks significant details.RenderPartial vs. RenderAction
If you use
Html.RenderPartial(), it will work faster, but you will have to provide the data for your partial views in a single controller action that returns the parent (partial)view.If you use
Html.RenderAction()it will work slower, but it gives you more flexibility and decoupling from the parent (partial)view, because data for a particular child partial view will be provided by always the same controller action that’s completely independent of parent (partial)view and controller action that returned it.How to choose
Both are correct. It all depends on the problem at hand.
If your partial views are not strongly typed and/or they are meant for posting you will most probably display them using
RenderPartial()extension method.But if they do consume some data and they are strongly typed and they are used on multiple completely different views it’s probably much better to use
RenderAction().Think of
RenderPartial()as a placeholder that just renders some view andRenderAction()as a separate MVC request pipeline. So before partial view gets rendered the whole request pipline is executed:So much more overhead.