Wednesday 20 June 2018

Partial View in MVC?

·        A partial view is a view that's rendered within another view. The HTML output generated by executing the partial view is rendered into parent view. Like views, partial views use the .cshtml file extension.
·        Partial view does not contain the layout page.
·        It does not verify for a viewstart.cshtml. We cannot put common code for a partial view within the viewStart.cshtml.page.
·        In MVC Partial view is designed specially to render within the view and just because of that it does not consist any mark up.
Uses
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.
It eliminates duplicate coding by reusing same partial view in multiple places. You can use the partial view in the layout view, as well as other content views.

Declaring Partial Views
Partial views are created like any other view: you create a .cshtml file within the Views folder. Underscore (_) is used before declaring partial views.
Example: _EmpDetails
Note:
If a partial view will be shared with multiple views of different controller folder then create it in the Shared folder, otherwise you can create the partial view in the same folder where it is going to be used.

Accessing Data from Partial Views:
We can access data from partial views using Html.Partial() and Html.RenderPartial().
1) Html.Partial()
@Html.Partial() helper method renders the specified partial view. It accept partial view name as a string parameter and returns MvcHtmlString. It returns html string so you have a chance of modifing the html before rendering.
Example:
@Html.Partial("_EmpDetails");  
2)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.
@Html.RenderPartial("_EmpDetails"); 
 
OR
@{
 Html.RenderPartial("_EmpDetails ");   
   
 }
 


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...