Work With Partial View in MVC Framework
Work With Partial View in MVC Framework
13,861,604
Sign up formembers
our free weekly Web Dev Newsletter. 283 Anderson Wernek
×
articles Q&A forums stuff lounge ? Search for articles, questions, tips
Follow
Introduction
As the complexity of a web application grows, we must decide when to create a new items and when to reuse them, if possible :).
Doing so keeps the application as simple and maintainable as possible. There for we should have a basic knowledge of the layout
and design structure of Html pages.
Descriptions
Like Master pages(web farms) or Layout(asp.net mvc) which offer a helpful way to reuse portion of markup and maintain a
consistent look and feel through out multiple pages in our site. But sometimes a scenario comes which may require more focused
approach to display some high level information in the multiple location but not in all places. To achieve this functionality Asp.net
MVC framework comes with solutions of Partial View, which lets you separate control of part of the page from the whole page, thus
enabling us to drop in consistent functionaltiy across multiple pages without having to rewrite code.
Partial views are views that contain targeted markup designed to be rendered as part of a large view. It does not specify any layout. or
we can say Partial views are the MVC replacement for user controls from ASP.NET Web Forms.
Lets go in details:
In this section, we will show you how to create and use partial views, explain how they work, and demonstrate the techniques
available for passing view data to a partial view.
1. Empty Partial View - When Creating Partial Views, if none of the Scaffold Template option is selected, then it will create an empty
partial view.
Later if we want to use model then we can attach like below syntax
2. Strongly Typed Partial View - When creating partial view, if we select or enter a type for the Model Class option then it will create
strongly typed partial view. Now partial view file is created and it only contains the @model tag to specify the view model type. Then
we have to pass view model objects when the partial view is rendered.
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 1/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
Hide Copy Code
@model ApplicationName.Models.ModelName
2. @{ Html.RenderPartial("PartialViewName", modelName); }
In the both cases first parameter is the name of the partial view and second paramter is the model name (not mandatory), when
second parameter is not specified, it defaults to the model in the view from which the Html.Partial() helper was called.
@Html.Partial() - it produces a fragment of HTML, rather than a full Html document. Partial helper return MVCHtmlString and
renders the partial view as an Html-encoded string.
@Html.RenderPartial- it doesnot return HTML markup like most other helper methods, instead it writes content directly to the
response stream, which is why we must call it like a complete line of C# using a semicolon (;).
Both way can be used to render partial view as part of view, only bit difference is performance. RenderPartial is more efficeint than
Partial
Controller.PartialView() - it creates a PartialViewResult object that renders a partial view. The PartialViewResult renders only the
markup in the view itself and does not render any layout or master page that the view may specify. Since partial pages do not
execute the layout, you may have to include some dependencies, such as css or Javascript, directly in the partial view rather than
including them in the page's layout.
Lets demonstrate
1. Create a demo web project to demonstrate partial view. File → New Project → Select Asp.net MVC framework and name your
project and select location.
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 2/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 3/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
2. Create Home controller in controller folder, follow below snap.
3. Create model class -> right-click on model folder and Add-> select class then create a model class.
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 5/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
4. Create a new folder in named as Home under Views folder and create a Index view
@model PartialViewDemo.Models.Company
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<h1>This is from main view upper.</h1>
</div>
<div>
@Html.Partial("_MyPartialView", Model.Department )
@{
Html.RenderPartial("_MyPartialView", Model.Department);
}
</div>
<div>
<h1>This is from main view lower.</h1>
</div>
<div id="divTest">
</div>
<input type="button" value="Click" id="btnClick"/>
</body>
<script src="~/Content/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
$('#btnClick').click(function(data) {
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 6/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
$.post("@Url.Action("ShowPartailView", "Home")", function(data) {
if (data) {
$('#divTest').append(data);
}
});
});
});
</script>
</html>
5. Create a new folder named as shared under Views folder - the partial view , layout or master pages are stored in shared folder
because these are shareable and can be used repeatedly. And then add two partial view (i) _MyPartialview and MySecondPartialView.
@model List<PartialViewDemo.Models.Department>
<h6> This below table content is from partial view</h6>
@if (Model != null)
{
<div>
<table cellspacing="0"width="50%" border="1">
<thead>
<tr>
<th>
Department Name
</th>
<th>
Department Rule
</th>
<th>
Admin Comment
</th>
</tr>
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 7/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
</thead>
<tbody>
@foreach (var dept in Model)
{
<tr>
<td align="center">
@dept.DepartmentName
</td>
<td align="center">
@dept.DepartmentRule
</td>
<td align="center">
@dept.AdminComment
</td>
</tr>
}
</tbody>
</table>
</div>
}
Now we are done with our coding part. Let debug this. Run And check.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
ASP.NET MVC Special Views - Partial View and ASP.NET MVC 2 Voting Control
Layout
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 8/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
Nice Artical we can add pratial view by click button but if we want on singal click of
typeview want submit all patial view data it it possible
Member 11082591 7-Nov-15 5:44
in type view I have added mutple patial view by button click after I want on typeview button click want singla type view as a
object whole as list it is possible
Great work!
skippyV 20-Oct-15 13:08
For those who follow the steps instead of just downloading the code, you might want to fix a few copy/paste errors. The
page has these small foopahs:
Hide Copy Code
Declaration needs capital 'D' in Company Model
public List<Department> GetDepartmentList()
And this extraneous text was pasted after code block before step #5:
<meta content="width=device-width" name="viewport" /> <title>
My vote of 5
khanzzirfan 11-Apr-15 22:15
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 9/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
Make your self a void pointer so that you can type cast it every data type.
Make your self a void pointer so that you can type cast it every data type.
Very Good... :)
jnyanendra 6-Oct-14 14:58
Very Good...
Thanks Jnyanendra
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 10/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
Thanks Sagar
My Vote 5
Shemeemsha (െഷമീംഷ) 24-Sep-14 6:39
Good Article
Re: My Vote 5
khem thapa 25-Sep-14 8:20
Thanks Shemeemsha
Regards, Khem
Nice details
Imran Abdul Ghani 23-Sep-14 3:51
Reply · View Thread
Nice
Dukhabandhu Sahoo 22-Sep-14 13:59
Re: Nice
khem thapa 23-Sep-14 4:15
Thanks Dukhabandhu
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 11/12
17/02/2019 Work with Partial view in MVC framework - CodeProject
Downloadlink
Member 10645579 22-Sep-14 11:19
Re: Downloadlink
khem thapa 22-Sep-14 11:30
Thank you user. Waiting for new version to reflect, as already updated with link to download.
My vote of 5
Humayun Kabir Mamun 22-Sep-14 8:20
Nice...
Re: My vote of 5
khem thapa 22-Sep-14 9:11
Thank you Humayun, Comments and suggestion are always appreciated Regards
Refresh 1
General News Suggestion Question Bug Answer Joke Praise Rant Admin
https://www.codeproject.com/Articles/821330/Work-with-Partial-view-in-MVC-framework 12/12