Click here to Skip to main content
15,887,346 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET MVC- Partial Views For Beginners

Rate me:
Please Sign up or sign in to vote.
4.45/5 (9 votes)
28 Oct 2014CPOL2 min read 29.5K   17   5
This article will help you to understand and create partial view in short and easy way.

Introduction

In this article, we will learn all about partial views, which will include where to use it, how to user it and different approaches to use it.

What is Partial View:

Partial View is a special kind of view used in MVC Framework, It is a part of the full view and that is why it is called as Partial.

Image 1

(Image to describe partial view)

In the above image, the gray area section is the main view area where we are using two partial views(Partial View 1 & Partial View 2).

Advantages of Partial View

  1. It is reusable. For Example, you can use same partial view in header and footer section of your master page, if the model is same in those section.

  2. Easy to handle/modify. Partial view make the application maintenance easier if in case you need any change in the structure, in that case we don’t need to change the entire page structure rather we can simple change on that particular Partial view where we need the changes.

Let's Create Our First Partial View

Select any view folder and then right click → Add → View → then the below dialog box will appear, name your view as _StudentDetails (Generally the partial view is starts with underscore to identify that it is a partial view and can be reused) and select the check box 'Create as partial view' and then click on 'Add' button. It will create the specified partial view.

Image 2

Now add a class in your Models folder. In our example we are creating a Student.cs file like this,

C#
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
}

Now create an action in your controller which will call this partial view, the name can be same or can be different, in our case we are using the same name for simplicity,

Image 3

Now, let add some details on that partial view, the class we have created can be supplied here,

Image 4

Now, lets design our Partial view page and show the details there,

Image 5

Calling/Using of Partial View:-

If in your current view you have the model values which will be displayed/used in the partial view, then you can directly call the view like this.

C#
@Html.Partial("_StudentDetails", Model)

Or

C#
@Html.RenderPartial("_StudentDetails", Model)

​If you do not have the model values, then you need to call the action associated with the partial view and it will return the partial view,

Razor
@{
       Html.RenderAction("_StudentDetails");
 }

These are the simple steps to create and use partial view which will give us better re-usability, and maintenance of code.

History

Last Update: 29th Oct, 2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Mindfire Solutions
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionA little bit rubbish Pin
Member 987309319-Jun-17 20:50
Member 987309319-Jun-17 20:50 
GeneralMy vote of 1 Pin
dr.samuel.john31-Oct-14 0:50
dr.samuel.john31-Oct-14 0:50 
QuestionPutting this in a _Layout Pin
CockroachCoader30-Oct-14 21:38
CockroachCoader30-Oct-14 21:38 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun29-Oct-14 19:29
Humayun Kabir Mamun29-Oct-14 19:29 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.