Presentation is loading. Please wait.

Presentation is loading. Please wait.

MVC Partial View.

Similar presentations


Presentation on theme: "MVC Partial View."— Presentation transcript:

1 MVC Partial View

2 What is a Partial View? A partial view is a view that is rendered within another view. The HTML output generated by executing the partial view is rendered into the calling (or parent) view. Like views, partial views use the .cshtml file extension.

3 When Should I Use Partial Views?
Partial views are an effective way of breaking up large views into smaller components. They can reduce duplication of view content and allow view elements to be reused. Common layout elements should be specified in _Layout.cshtml. Non-layout reusable content can be encapsulated into partial views. If you have a complex page made up of several logical pieces, it can be helpful to work with each piece as its own partial view. Each piece of the page can be viewed in isolation from the rest of the page, and the view for the page itself becomes much simpler since it only contains the overall page structure and calls to render the partial views.

4 Declaring Partial Views
Partial views are created like any other view: you create a .cshtml file within the Views folder. There is no semantic difference between a partial view and a regular view - they are just rendered differently. You can have a view that is returned directly from a controller’s ViewResult, and the same view can be used as a partial view. The main difference between how a view and a partial view are rendered is that partial views do not run _ViewStart.cshtml

5 Accessing Data From Partial Views
When a partial view is instantiated, it gets a copy of the parent view's ViewData dictionary. Updates made to the data within the partial view are not persisted to the parent view. ViewData changed in a partial view is lost when the partial view returns. By default, the view engine passes each partial view as a reference to the same view model object it received from the controller. It’s a simple matter of sharing the view context and including system-defined data dictionaries such as ViewData and ViewBag as well.

6 Rendering Partial Views
You can render the partial view in the parent view using html helper methods: Partial() RenderPartial() RenderAction()

7 Html.Partial() @Html.Partial() helper method renders the specified partial view. It accepts a partial view name as a string parameter and returns MvcHtmlString. Returns a html string so you have a chance of modifying the html before rendering.

8 Overloads of the Partial Method
Helper Method Description MvcHtmlString Html.Partial(string partialViewName) Renders the given partial view content in the referred view. MvcHtmlString Html.Partial(string partialViewName,object model) Renders the partial view content in the referred view. Model parameter passes the model object to the partial view. MvcHtmlString Html.Partial(string partialViewName, ViewDataDictionary viewData) Renders the partial view content in the referred view. View data parameter passes view data dictionary to the partial view. MvcHtmlString Html.Partial(string partialViewName,object model, ViewDataDictionary viewData) Renders the partial view content in the referred view. Model parameter passes the model object and View data passes view data dictionary to the partial view.

9 Html.RenderPartial() The RenderPartial helper method is same as the Partial method except that it returns void and writes resulted html of a specified partial view into a http response stream directly.

10 Overloads of the RenderPartial Method
Helper method Description RenderPartial(String partialViewName) Renders the specified partial view RenderPartial(String partialViewName, Object model) Renders the specified partial view and set the specified model object RenderPartial(String partialViewName, ViewDataDictionary viewData) Renders the specified partial view, replacing its ViewData property with the specified ViewDataDictionary object. RenderPartial(String partialViewName, Object model, ViewDataDictionary viewData) Renders the specified partial view, replacing the partial view's ViewData property with the specified ViewDataDictionary object and set the specified model object

11 Html.RenderAction() The RenderAction helper method invokes a specified controller and action and renders the result as a partial view. The specified Action method should return PartialViewResult using the PartialView() method.

12 Overloads of the RenderAction Method
Name Description RenderAction(String actionName) Invokes the specified child action method and renders the result in the parent view. RenderAction(String actionName, Object routeValue) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view. RenderAction(String actionName, String controllerName) Invokes the specified child action method using the specified controller name and renders the result inline in the parent view. RenderAction(String actionName, RouteValueDictionary routeValues) RenderAction(String actionName, String controllerName, Object routeValue) Invokes the specified child action method using the specified parameters and controller name and renders the result inline in the parent view. RenderAction(String actionName, String controllerName, RouteValueDictionary routeValues)

13 Difference between Html. Partial() and Html. RenderPartial() in ASP
Difference between Html.Partial() and Html.RenderPartial() in ASP.NET MVC Html.Partial() Html.RenderPartial() Html.Partial returns html string. Html.RenderPartial returns void. Html.Partial injects html string of the partial view into main view. Html.RenderPartial writes html in response stream. Performance is slow. Perform faster than HtmlPartial(). Html.Partial() need not to be inside the braces. Html.RenderPartial must be inside }.


Download ppt "MVC Partial View."

Similar presentations


Ads by Google