开发者

how to display using asp.net mvc

I have a partial vi开发者_C百科ew 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 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.


Try

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜