Differences between Html. RenderPartial and Html. Partial in the Razor View

Source: Internet
Author: User

Both of them can output a Partial view. The difference is as follows:
1. Partial has a return value (MvcHtmlString); RenderPartial has no return value (Void ).
 
Definition
1 // PartialExtensions. Partial method (HtmlHelper, String)
2 public static MvcHtmlString Partial (
3 this HtmlHelper htmlHelper,
4 string partialViewName
5)
6
7 // RenderPartialExtensions. RenderPartial method (HtmlHelper, String)
8 public static void RenderPartial (
9 this HtmlHelper htmlHelper,
10 string partialViewName
11)

 
2. Partial output to the Temporary Variable StringWriter; RenderPartial output to HtmlHelper. ViewContext. Writer (that is, directly output to Response ).
Implement www.2cto.com in 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 ());
}
}
 

 
Internal Implementation of 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. the syntax in the Razor view is different:
Syntax
1 @ Html. Partial ("PartialViewName ")
2
3 @ {Html. RenderPartial ("PartialViewName ");}
 

 

From Rickey Hu

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.