Tuesday, 27 December 2016

Different methods used to render the views and Partial views in ASP.Net MVC


1. RenderPartial  - RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.
@{Html.RenderPartial("_partialView");}
This method returns void. And simple to use and no need to create any action.
This method is faster than Partial method since its result is directly written to the response stream which makes it fast.

2. Partial - Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model.
@Html.Partial("_partialView")
Simple to use and no need to create any action.This method result can be stored in a variable, since it returns string type value.
Renders the partial view as an HTML-encoded string.

3. RenderAction - RenderAction method is useful when the displaying data in the partial view is independent from corresponding view model.
@{Html.RenderAction("Action_Name","Controller-Name");}
For this method, we need to create a child action for the rendering the partial view.
This method is the best choice when you want to cache a partial view.
This method is faster than Action method since its result is directly written to the HTTP response stream which makes it fast.

4. Action - Action method is useful when the displaying data in the partial view is independent from corresponding view model.
@Html.Action("Action_Name","Controller-Name");
For this method, we need to create a child action for the rendering the partial view.This method result can be stored in a variable, since it returns string type value.
Renders the partial view as an HtmlString .

This method is also the best choice when you want to cache a partial view.

5. Direct return from Controller 


public ActionResult Action()
        {
            return PartialView("PartialViewName");
OR
            return View("ViewName");

        }

For More check Views Partial Views

1 comment:

Note: only a member of this blog may post a comment.