Q: What is partial view?
Ans: A partial view is a customized shared view, which can’t be served directly and can be used in other views. i.e Reusable custom view.
Q: What are the advantages with partial views?
Ans: 1. Sometimes you need to return just a piece and you can do it by a async request (using ajax). In these cases, you use an action that returns a Partial View.
2. Sometimes you need to implement an View that need to add a piece of code, so, you can render it using a Partial View.
3. Using partial views you can avoid code redundancy, i.e partial view are reusable
Q: How to Implement partial view in MVC
Ans: Here we will learn how to implement partial view in mvc with product catalog example.
let’s create product catalog data first
To reate product catalog data first create model as follows
Now create product catalog data
Now create one action method in controller as follows
Now add a view for the above action as follows
Right click on the action method and choose addview…
Click add to create view.
Write the following code in the created view
In the above code @html.Partial is a helper class and we can use this to render our partial view. For this helper we are passing two parameters
1.”_ProductBox”: partial view name
2.product : Model for the partial view
Now let’s add the partial view in shared folder under views as follows
RightClick on Shared folder under views and choose add then view and fill the following details
After adding this partial view write the following code into it.
Now Rebuild the application and run