转载:http://blog.csdn.net/kufeiyun/article/details/9377065
MVC Razor中有不同的展现partial view的方法,许多开发人员子在选择使用 RenderPartial or RenderAction or Partial or Action helper 方法时比较困惑,不知该选择哪一个,这篇文章,我向大家介绍一下Html.RenderPartial, Html.RenderAction, Html.Partial & Html.Action的不同
Html.RenderPartial
-
这个方法会直接将结果写入到当前请求的http response数据流中,这意味着它使用了和当前webpage/template使用的相同的TextWriter对象
-
方法没有返回值
-
不需要创建action,使用简单
-
如果和partial view 对应的view model中,已经存在了partial view展示所需数据,RenderPartial方法会很有用.For example :blog中,显示一篇文章和它的评论, RenderPartial 方法会是比较好的选择,既然文章的评论信息已经存在在view model中
<ol class="linenums" style="margin-top: 0px; margin-bottom: 0px; color: rgb(157, 157, 157);"><li class="L0" style="padding: 5px 0px; text-align: justify;"><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">@{</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">Html</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">.</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">RenderPartial</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">(</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"_Comments"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">);}</span></li></ol>
-
这个方法比partial 方法更快,因为它直接将结果系统到当前响应的数据流中
Html.RenderAction
-
和上一个一样,执行结果会直接写入当前响应的数据流中
-
需要创建child action 方法.
-
如果partial view 的数据独立于对应的view的 viewmodel,则这个方法比较有用,For example : Iblog中每个页面都显示不同的类目菜单,我们最好选择使用RenderAction 既然类目数据是由不同的model提供
<ol class="linenums" style="margin-top: 0px; margin-bottom: 0px; color: rgb(157, 157, 157);"><li class="L0" style="padding: 5px 0px; text-align: justify;"><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">@{</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">Html</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">.</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">RenderAction</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">(</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"Category"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">,</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"Home"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">);}</span><span class="pln" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);"> </span></li></ol>
-
如果你想缓存partial view,这是最好的选择。
-
这个方法比action方法快,基于第一条原因
Html.Partial
-
结果以HTML-encoded 字符串展示
-
返回的是string类型,所以结果可以存储在变量里.
-
使用简单,无需创建action
-
Partial method is useful used when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.
<ol class="linenums" style="margin-top: 0px; margin-bottom: 0px; color: rgb(157, 157, 157);"><li class="L0" style="padding: 5px 0px; text-align: justify;"><span class="lit" style="vertical-align: top; margin: 0px; color: rgb(0, 126, 253);">@Html</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">.</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">Partial</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">(</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"_Comments"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">)</span></li></ol>
Html.Action
-
结果直接展示为HtmlString .
- 需要创建对应的child action
- 返回字符串,可以存储在变量里.
-
Action method is useful when the displaying data in the partial view is independent from corresponding view model.For example : In a blog to show category list on each and every page, we would like to use Action method since the list of category is populated by the different model.
<ol class="linenums" style="margin-top: 0px; margin-bottom: 0px; color: rgb(157, 157, 157);"><li class="L0" style="padding: 5px 0px; text-align: justify;"><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">@{</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">Html</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">.</span><span class="typ" style="vertical-align: top; margin: 0px; color: rgb(83, 83, 166);">Action</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">(</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"Category"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">,</span><span class="str" style="vertical-align: top; margin: 0px; color: rgb(255, 104, 32);">"Home"</span><span class="pun" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);">);}</span><span class="pln" style="vertical-align: top; margin: 0px; color: rgb(57, 49, 36);"> </span></li></ol>
-
可以缓存partial view.