Take a look at GW

【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的三个参数的用法 用法实例

posted @ 2020-06-29 14:12  HDWK  阅读(501)  评论(0编辑  收藏  举报