Html.RenderPartial和Html.Partial区别

  1. 两者都可以输出一个Partial视图,区别如下

  2者相关源码如下:

View Code
public static class PartialExtensions

    {

        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName)

        {

            return Partial(htmlHelper, partialViewName, null /* model */, htmlHelper.ViewData);

        }



        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData)

        {

            return Partial(htmlHelper, partialViewName, null /* model */, viewData);

        }



        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model)

        {

            return Partial(htmlHelper, partialViewName, model, 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());

            }

        }

    }
View Code
public static class RenderPartialExtensions

    {

        // Renders the partial view with the parent's view data and model

        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName)

        {

            htmlHelper.RenderPartialInternal(partialViewName, htmlHelper.ViewData, null /* model */, htmlHelper.ViewContext.Writer, ViewEngines.Engines);

        }



        // Renders the partial view with the given view data and, implicitly, the given view data's model

        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData)

        {

            htmlHelper.RenderPartialInternal(partialViewName, viewData, null /* model */, htmlHelper.ViewContext.Writer, ViewEngines.Engines);

        }



        // Renders the partial view with an empty view data and the given model

        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model)

        {

            htmlHelper.RenderPartialInternal(partialViewName, htmlHelper.ViewData, model, htmlHelper.ViewContext.Writer, ViewEngines.Engines);

        }



        // Renders the partial view with a copy of the given view data plus the given model

        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)

        {

            htmlHelper.RenderPartialInternal(partialViewName, viewData, model, htmlHelper.ViewContext.Writer, ViewEngines.Engines);

        }

    }

 

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

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

 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());

            }

        }
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)

        {

            htmlHelper.RenderPartialInternal(partialViewName, viewData, model, htmlHelper.ViewContext.Writer, ViewEngines.Engines);

        }

 

2. 在Razor视图中的使用方式

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

 

3.参考网址

http://blog.csdn.net/fer_ba/article/details/7669033 

 

4.简单Demo

 MVCCommonSolution_01.rar

你可能感兴趣的:(html)