• 【ASP.NET】Html.Partial和Html. RenderPartial用法


    1. Html.Partial和Html. RenderPartial区别 

    Html.partial和RenderPartial都是输出html片段,区别在于 Partial是将视图内容直接生成一个字符串并返回(相当于有个转义的过程),RenderPartial方法是直接输出至当前 HttpContext(因为是直接输出,所以性能好)。因此它们在视图中的使用方式是不同的:
    MVC3:

    @Html.Partial("BasicChart")
    @{
    Html.RenderPartial("BasicChart");
    }

    Html.partial和RenderPartial的其它三个重载很有用,第二个重载 @{Html.RenderPartial("BasicChart",model);} 
    用这个重载可以在部分视图里使用强类型,然后在主视图中使用第二个参数传model过去,而不用controller
    比如从list中传其中一项myClass过去
    第三个重载用来传ViewData同理,如: @{Html.RenderPartial("BasicChart",ViewData["myData"]);} 

    示例:

    <div id="logindisplay">
    
                    @Html.Partial("_LogOnPartial")*@
    
                    @{
    
                        Html.RenderPartial("_LogOnPartial");
                     }
    
                </div>

    2. Http.RenderPartial的三个参数的用法 用法实例

    后台代码:

                ViewBag.smallCategory = categoryService.LoadEntities(m=>m.ParentID==(short)Model.Enum.CategoryEnum.ProductTrade);
                ViewData["Total"] = classInfoService.LoadEntities(m => m.Category.ParentID == (short)Model.Enum.CategoryEnum.ProductTrade).ToList().Count;

    前台代码:

    @{
         var data = new ViewDataDictionary();
         data.Add("total",  ViewData["Total"]);
         Html.RenderPartial("../Category/SmallCategory", ViewBag.smallCategory as IEnumerable<Jyson.ZhanShiQuan.Model.Category>, data);
    }

    绑定的页面代码:

    @model  IEnumerable<Jyson.ZhanShiQuan.Model.Category>
    @{
        Layout = null;
    }
    <div class="sxbox">
       <div class="where">
            <div class="btn-pub"><span style="color: #BC2D15; font-weight: 600;"> @Html.ViewData["total"] </span>条信息</div>
            <ul class="area_house">
                <li class="choose_on"><strong><span class="fc-gray">所有分类</span></strong></li>
            </ul>
        </div>
        <div class="change_county">
            <ul>
                <li><a id="0" href="../ProductTrade/List">全部</a></li>
                @foreach (var item in Model)
                {
                    <li><a id="@item.ID" href="../ProductTrade/List-@item.ID">@item.CName</a></li>
                }
            </ul>
            <div class="clear">
            </div>
        </div>
    </div>

    参考文章:

    1. Html.partial和RenderPartial的用法与区别

    2. Html.RenderPartial的三个参数的用法 用法实例

  • 相关阅读:
    Magic-Club第六天
    .net工具类——文件操作
    .net工具类——HTML处理
    .net工具类——随机生成
    .net工具类——删除最后结尾的一个逗号
    .net工具类——分割字符串
    .net工具类——对象转换处理
    .net扩展方法——其他(科学计数法、ToDictionary 去重、List<Guid>转为List<Guid?>)
    .net扩展方法——类型转换
    『Linux学习笔记』7. 管道和过滤器 -- pipe
  • 原文地址:https://www.cnblogs.com/HDK2016/p/13207733.html
Copyright © 2020-2023  润新知