MVC Partial View What is a Partial View













- Slides: 13

MVC Partial View

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.

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.

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 View. Result, 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 _View. Start. cshtml

Accessing Data From Partial Views • When a partial view is instantiated, it gets a copy of the parent view's View. Data dictionary. • Updates made to the data within the partial view are not persisted to the parent view. • View. Data changed in a partial view is lost when the partial view returns.

Rendering Partial Views • You can render the partial view in the parent view using html helper methods: • Partial() • Render. Action()

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

Overloads of the Partial Method Helper Method Description Mvc. Html. String Html. Partial(string partial. View. Name) Renders the given partial view content in the referred view. Mvc. Html. String Html. Partial(string partial. View. Name, object model) Renders the partial view content in the referred view. Model parameter passes the model object to the partial view. Mvc. Html. String Html. Partial(string partial. View. Name, View. Data. Dictionary view. Data) Renders the partial view content in the referred view. View data parameter passes view data dictionary to the partial view. Mvc. Html. String Html. Partial(string partial. View. Name, object model, View. Data. Dictionary view. Data) 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.

Html. Render. Partial() • The Render. Partial 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.

Overloads of the Render. Partial Method Helper method Description Render. Partial(String partial. View. Name) Renders the specified partial view Render. Partial(String partial. View. Name, Object model) Renders the specified partial view and set the specified model object Render. Partial(String partial. View. Name, View. Data. Dictionary view. Data) Renders the specified partial view, replacing its View. Data property with the specified View. Data. Dictionary object. Render. Partial(String partial. View. Name, Object model, View. Data. Dictionary view. Data) Renders the specified partial view, replacing the partial view's View. Data property with the specified View. Data. Dictionary object and set the specified model object

Html. Render. Action() • The Render. Action helper method invokes a specified controller and action and renders the result as a partial view. • The specified Action method should return Partial. View. Result using the Partial. View() method.

Overloads of the Render. Action Method Name Description Render. Action(String action. Name) Invokes the specified child action method and renders the result in the parent view. Render. Action(String action. Name, Object route. Value) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view. Render. Action(String action. Name, String controller. Name) Invokes the specified child action method using the specified controller name and renders the result inline in the parent view. Render. Action(String action. Name, Route. Value. Dictionary route. Values) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view. Render. Action(String action. Name, String controller. Name, Object route. Value) Invokes the specified child action method using the specified parameters and controller name and renders the result inline in the parent view. Render. Action(String action. Name, String controller. Name, Route. Value. Dictionary route. Values) Invokes the specified child action method using the specified parameters and controller name and renders the result inline in the parent view.

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