0

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?

3
  • There are some problems with your post. Detail.ascx is not a controller, it's a view. Commented May 19, 2010 at 18:32
  • Yes sorry its view.. what post? Commented May 19, 2010 at 18:35
  • The 'Post' referred to is when you posted your question. In what way did the above not work? Commented May 19, 2010 at 19:25

2 Answers 2

2

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 and RenderAction() as a separate MVC request pipeline. So before partial view gets rendered the whole request pipline is executed:

  • request gets issued
  • controller action gets invoked, prepares data and
  • returns a partial view (that will be placed inside some other parent (partial)view)

So much more overhead.

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

Comments

0

Try

<% Html.RenderPartial("StudentInfo") %>

Comments

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.