MVC razor 中 RenderPartial, RenderAction , Partial , Action 的使用选择

转载: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

  1. 这个方法会直接将结果写入到当前请求的http response数据流中,这意味着它使用了和当前webpage/template使用的相同的TextWriter对象

  2. 方法没有返回值

  3. 不需要创建action,使用简单

  4. 如果和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>

  5. 这个方法比partial 方法更快,因为它直接将结果系统到当前响应的数据流中

Html.RenderAction

  1. 和上一个一样,执行结果会直接写入当前响应的数据流中

  2. 需要创建child action 方法.

  3. 如果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>

  4. 如果你想缓存partial view,这是最好的选择。

  5. 这个方法比action方法快,基于第一条原因

Html.Partial

  1. 结果以HTML-encoded 字符串展示

  2. 返回的是string类型,所以结果可以存储在变量里.

  3. 使用简单,无需创建action

  4. 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

  1. 结果直接展示为HtmlString .

  2. 需要创建对应的child action
  3. 返回字符串,可以存储在变量里.
  4. 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>

  5. 可以缓存partial view.

更好点的说明,可以参考其他的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值