Html.RenderPartial和Html.Partial在Razor视图中的区别

两者都可以输出一个Partial视图;其区别如下:

1. Partial有返回值(MvcHtmlString);RenderPartial没有返回值(Void)。

定义

1 //PartialExtensions.Partial 方法 (HtmlHelper, String)
2 public static MvcHtmlString Partial(
3 this HtmlHelper htmlHelper,
4 string partialViewName
5 )
6
7 //RenderPartialExtensions.RenderPartial 方法 (HtmlHelper, String)
8 public static void RenderPartial(
9 this HtmlHelper htmlHelper,
10 string partialViewName
11 )

2. Partial输出到临时变量StringWriter ;RenderPartial输出到HtmlHelper.ViewContext.Writer上(即直接输出到Response)。

Html.Partial内部实现

public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName)
{
return htmlHelper.Partial(partialViewName, null, htmlHelper.ViewData);
}
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)
{
using (StringWriter writer = new StringWriter(CultureInfo.CurrentCulture))
    {
        htmlHelper.RenderPartialInternal(partialViewName, viewData, model, writer, ViewEngines.Engines);
return MvcHtmlString.Create(writer.ToString());
    }
}

Html.RenderPartial内部实现

1 public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName)
2 {
3     htmlHelper.RenderPartialInternal(partialViewName, htmlHelper.ViewData, null, htmlHelper.ViewContext.Writer, ViewEngines.Engines);
4 }

3. 在Razor视图中语法不一样:

语法

1 @Html.Partial("PartialViewName")
2
3 @{ Html.RenderPartial("PartialViewName"); }

物流,配货,货运,网站,论坛,交流,信息发布
网站建设QQ:471226865

你可能感兴趣的:(html)