10
Reply

What is difference between @Html.Partial and @Html.RenderPartial ?

Yogesh Bajpai

Yogesh Bajpai

10y
58.6k
0
Reply

    The main difference is that "RenderPartial" returns void and output will be written directly to the output sream, where as the "Partial" returns MvcHtmlString which can be assigned to a variable and manipulate it if required. So, when there is no need to assign the output to a variable for manipulating it, then use Partial, else use RenderPartial. Renderpartial does exactly the same thing and is better for performance over partial().

    Html.Partial returns a String, Html.RenderPartial calls Write internally, and returns void. The basic usage is: // Razor syntax @Html.Partial("ViewName") @{ Html.RenderPartial("ViewName"); } // WebView syntax <%: Html.Partial("ViewName") %> <% Html.RenderPartial("ViewName"); %> In the snippet above, both calls will yield the same result. While one can store the output of Html.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial. The result will be written to the Response stream during execution/evaluation.

    The main difference between Partial and RenderPartial is : Partial return string and write it to document as Shweta said . RenderPartial actually write direct to context response.

    // Html.RenderPartial(1) This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.(2) This method returns void.(3) Simple to use and no need to create any action.(4) RenderPartial method is useful used when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.EX: @{Html.RenderPartial("_Comments");}(5) This method is faster than Partial method since its result is directly written to the response stream which makes it fast.// Html.Partial(1) Renders the partial view as an HTML-encoded string.(2) This method result can be stored in a variable, since it returns string type value.(3) Simple to use and no need to create any action.(4) Partial method is useful used when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.EX: @Html.Partial("_Comments")

    Which one is better ?

    http://www.youtube.com/watch?v=0fYzD-SUW0I

    Here is what I have found:Use RenderAction when you do not have a model to send to the view and have a lot of html to bring back that doesn't need to be stored in a variable.Use Action when you do not have a model to send to the view and have a little bit of text to bring back that needs to be stored in a variable.Use RenderPartial when you have a model to send to the view and there will be a lot of html that doesn't need to be stored in a variable.Use Partial when you have a model to send to the view and there will be a little bit of text that needs to be stored in a variable.RenderAction and RenderPartial are faster.

    @Html.RenderPartial("_OrderPage") is better than @Html.Partial("_OrderPage")

    Html.RenderPartial1.Html.RenderPartial: This method result will be directly written to the HTTP response stream means it used the same Text Writer object as used in the current webpage/template. 2.This method returns void. 3.Simple to use and no need to create any action. 4.RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model. For example : In a blog to show comments of an article, we would like to use Render Partial method since an article information with comments are already populated in the view model. @{Html.RenderPartial("_Comments");} 5.This method is faster than Partial method since its result is directly written to the response stream which makes it fast.Html.Partial1. Renders the partial view as an HTML-encoded string. 2. This method result can be stored in a variable, since it returns string type value. 3. Simple to use and no need to create any action. 4. Like Render Partial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model. . @Html.Partial("_Comments")For Performance : @Html.RenderPartial("_OrderPage") is better than @Html.Partial("_OrderPage")

    Thanks Guys.