Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

asp.net - What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

Other than the type it returns and the fact that you call it differently of course

<% Html.RenderPartial(...); %>
<%= Html.Partial(...) %>  

If they are different, why would you call one rather than the other one?
The definitions:

// Type: System.Web.Mvc.Html.RenderPartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll

using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    public static class RenderPartialExtensions
    {
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName);
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData);
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model);

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

// Type: System.Web.Mvc.Html.PartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll

using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    public static class PartialExtensions
    {
        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName);

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

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

        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model,
                                        ViewDataDictionary viewData);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only difference is that Partial returns an MvcHtmlString, and must be called inside <%= %>, whereas RenderPartial returnsvoid and renders directly to the view.

If you look at the source code, you'll see that they both call the same internal method, passing a StringWriter for it to render to.

You would call Partial if you want to view, save, or manipulate the generated HTML instead of writing it to the page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...