The article is a bit long, but mostly code, look quickly, don't stress too much. There are a lot of differences on the internet about these two methods, all speak of its essential difference (do not look at the code, just look at this conclusion, it is enough, if you feel the need to draw this conclusion from the code, then continue to look down), here from the code to tell you the difference between the two extension methods are used to load a partial view First of all, the difference in their nature is that renderpartial returns a view directly, while partial returns a string of views, a return view object, and a return string. What does the interior do? The next step is described in detail.
1.Partial
The extension class is described in the Anti-compilation tool as follows: represents the functionality to render a partial view as an html-encoded string. Probably means to generate a partial page as an HTML character String (English is better, re-translate, haha).
This method is HtmlHelper extension method, this method has four overloads, with the reflector tool to see, the code is as follows:
Public Static classpartialextensions{//Methods Public StaticMvchtmlstring Partial ( ThisHtmlHelper HtmlHelper,stringpartialviewname); Public StaticMvchtmlstring Partial ( ThisHtmlHelper HtmlHelper,stringPartialviewname,Objectmodel); Public StaticMvchtmlstring Partial ( ThisHtmlHelper HtmlHelper,stringpartialviewname,viewdatadictionary viewData); Public StaticMvchtmlstring Partial ( ThisHtmlHelper HtmlHelper,stringPartialviewname,Objectmodel, Viewdatadicti onary viewData);}
The four methods are the same, the difference is passed to their parameters (different parameters are we pass to the view data ViewData and model), here is the first and the fourth method, the others are the same.
(1) Partial of two parameters
The code in the Anti-compilation tool is as follows:
Public Static Mvchtmlstring Partial (Thisstring partialviewname) { returnnull , Htmlhelper.viewdata); }
The first parameter is the syntax of the extension method, which is not said here. This method calls a partial method, which is the native method in the HtmlHelper, and goes inside to see what it has done:
Public Static Mvchtmlstring Partial (Thisstringobject model, viewdatadictionary viewData) { usingnew StringWriter (cultureinfo.currentculture)) { Htmlhelper.renderpartialinternal (Partialviewname, ViewData, model, writer, viewengines.engines); return Mvchtmlstring.create (writer. ToString ());} }
Obviously, it uses a stringwriter, see this must know is related to the string, and again the two methods called in the function (directly in the code to explain):
Internal Virtual voidRenderpartialinternal (stringPartialviewname, Viewdatadictionary ViewData,Objectmodel, TextWriter writer, viewenginecollection viewenginecollection) { if(string. IsNullOrEmpty (Partialviewname)) {Throw NewArgumentException (Mvcresources.common_nullorempty,"Partialviewname"); } viewdatadictionary Dictionary=NULL; if(model = =NULL) { if(ViewData = =NULL) {Dictionary=NewViewdatadictionary ( This. ViewData);//These steps must have been filling in our data, the same as}Else{Dictionary=Newviewdatadictionary (ViewData); } } Else if(ViewData = =NULL) {Dictionary=Newviewdatadictionary (model); } Else{viewdatadictionary Dictionary2=Newviewdatadictionary (viewData) {//I don't understand the role of this now, understand the comrades, ask the Model=model}; Dictionary=Dictionary2; } viewcontext ViewContext=NewViewContext ( This. ViewContext, This. Viewcontext.view, Dictionary, This. ViewContext. TempData, writer); Findpartialview (ViewContext, Partialviewname, viewenginecollection). Render (ViewContext, writer);
This method is used to generate the view, and the specific code looks down. The Render method is to say that the view returned by the Findpartialview method is written to the writer stream, further reading the Findpartialview code.
}
Look at this code again, Mvchtmlstring.create (writer. ToString ()), look at this and everyone will understand:
It calls this method first
publicstatic mvchtmlstring Create (string value) { returnnew public mvchtmlstring (stringbasestring. Empty) { thisstring. Empty;}
The most essential things everyone saw, and finally say Findpartialview this method (direct comment code):
Internal StaticIView Findpartialview (ViewContext viewcontext,stringPartialviewname, Viewenginecollection viewenginecollection) {Viewengineresult result=Viewenginecollection.findpartialview (ViewContext, partialviewname);//search for the name Partialviewname view from the View engine collectionif(result.) View! =NULL)//If the view is found, directly return the view {returnresult. View; } StringBuilder Builder=NewStringBuilder ();//The code will not say, unintelligible, know the friend, ask for adviceforeach(stringStrinchresult. searchedlocations) {Builder. Appendline (); Builder. Append (str); } Throw NewInvalidOperationException (string. Format (CultureInfo.CurrentCulture, Mvcresources.common_partialviewnotfound,New Object[] {partialviewname, builder});}
After executing the Findpartialview, a view is returned, and the Render method is returned after the view, and the Render method should be written to the writer (the Anti-compilation tool has no view, the principle should be).
(2) Partial of four parameters
Look directly at the code, we are clear:
Public Static Mvchtmlstring Partial (Thisstringobject model, viewdatadictionary viewData) { usingnew StringWriter (cultureinfo.currentculture)) { Htmlhelper.renderpartialinternal (Partialviewname, ViewData, model, writer, viewengines.engines); return Mvchtmlstring.create (writer. ToString ());} }
Code is like this, is not feeling familiar with, in fact, as mentioned above the code, the only difference is that there is no incoming data. For more information, please refer to the previous Renderpartialinternal code, which is to reload the data and then call the Generate view Findpartialview method, specifically see above ha, here is not much to say.
The partial method says here.
2.RenderPartial
The extension class is described in the Anti-compilation tool as follows: Provides support for rendering a partial view. The meaning is probably to provide a method that supports generating partial pages.
This method is HtmlHelper extension method, this method also has four overloads, with the reflector tool to see, the code is as follows:
Public Static classrenderpartialextensions{//Methods Public Static voidRenderPartial ( ThisHtmlHelper HtmlHelper,stringpartialviewname); Public Static voidRenderPartial ( ThisHtmlHelper HtmlHelper,stringPartialviewname,Objectmodel); Public Static voidRenderPartial ( ThisHtmlHelper HtmlHelper,stringPartialviewname, Viewdatadictionary viewData); Public Static voidRenderPartial ( ThisHtmlHelper HtmlHelper,stringPartialviewname,Objectmodel, Viewdatadictionary viewData);}
Does it feel similar to the partial method, except that the name is different? Yes, not the same is the method in which it is called, which is said to be one of the four extension methods (they call the same method, but there is no difference in data), the first method code is as follows:
Public Static void RenderPartial (Thisstring partialviewname) { null, HtmlHelper.ViewContext.Writer, Viewengines.engines);}
The essential difference is that after executing the Renderpartialinternal method, no Mvchtmlstring.create (writer) is executed. ToString ()) This method, which is said to be the partial is the return string, and renderpartial return is the reason for the view. The renderpartialinternal code is the same as the renderpartialinternal of the partial method call. The difference is straightforward, such as 1:
Figure 1
As you can see, partial writes the view to the stream after the view is generated. The Mvchtmlstring.create method is then converted to a string (specifically the code in the partial), and RenderPartial has no other action after calling the Renderpartialinternal method (Renderpartialinterna L called the Finderpartialview method, which returns view in this method.
The above is the difference between the two methods, do not know that there is no clear, of course there are some things I do not know, if we are aware of those things, you can put forward to talk about, learn from each other, thank you.
Html.partial ("") differs from Html.renderpartial ("")