zoukankan      html  css  js  c++  java
  • mvc razor中renderPartial,RenderAction,Partial,Action的使用选择

    RenderPartial:

      通常被用来显示一个功能相对独立的“块”,比如说显示菜单或者导航条。 RenderPartial输出的结果被作为调用的View的一部分显示。

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

    2. 方法没有返回值

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

    4. 如果和partial view 对应的view model中,已经存在了partial view展示所需数据,RenderPartial方法会很有用.For example :blog中,显示一篇文章和它的评论, RenderPartial 方法会是比较好的选择,既然文章的评论信息已经存在在view model中

      1. @{Html.RenderPartial("_Comments");}
    5. 这个方法比partial 方法更快,因为它直接将结果系统到当前响应的数据流中

    RenderAction:

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

    2. 需要创建child action 方法.

    3. 如果partial view 的数据独立于对应的view的 viewmodel,则这个方法比较有用,For example : Iblog中每个页面都显示不同的类目菜单,我们最好选择使用RenderAction 既然类目数据是由不同的model提供

      1. @{Html.RenderAction("Category","Home");}
    4. 如果你想缓存partial view,这是最好的选择。

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

      

    Partial:Partial 回传的一个Object (MvcHtmlString), 回传一个String 把一堆Html给回传出来, 然后写进到主页面上

    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.

    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.

      1. @{Html.Action("Category","Home");}
    5. 可以缓存partial view.

    return界面:

    PartialView():

    值的缓存:
    ViewBag.Test






  • 相关阅读:
    Ubuntu18.04查看ip地址
    使用Vmware克隆功能快速创建多台虚拟机
    使用Vmware快照功能对虚拟机进行备份还原
    安装Vmware并创建Ubuntu虚拟机
    使用vmware+Ubuntu搭建hadoop集群
    Gitee图床+PicGo+Typora便捷在博客中使用图片
    使用Gitee Pages+hugo免费搭建你的博客
    Scheduler的WaitRun存在卡死的问题
    使用OpenJDK进行Delphi Android开发
    citus
  • 原文地址:https://www.cnblogs.com/jacketlin/p/5562821.html
Copyright © 2011-2022 走看看