To create the reusable components we will use the Partial Views. There are two types of Partial Views:
Static partial view: It will return static data
@html.RenderPartial(“Partial view”); Will return void displays ouput on viewpage.
@html.Partial(“partialview”): Return MVC HTML string , you can store it in particular variable.
Dynamic partial view: To implement dynamic data
@html.RenderAction(“partialview”)
@html.Action(“partialview”):Will return MVC html string
Let us see partial view example step by step:-
Step 1:
Create new project called "AspnetMVCPartialViewDemo"
Step 2:
Now we are going to create 2 partial views , one is Navigationbar partial view and another one is FooterPartial view.
Step 3:
Go to Shared folder and create NavBarPartialview.cshtml
Step 4:
Create another partial view called "FooterView.cshtml"
Step 5:
Eventually modify _Layout.cshtml to invoke both Navigationbar and Footer partial views.
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
@{
var disppartialview = Html.Partial("_NavBarPartialView");
}
@disppartialview
<div>
<div class="container body-content">
@RenderBody()
<hr >
@{Html.RenderPartial("_FooterView"); }
<div>
<body>
Step 6:
Run AspnetMVCPartialViewDemo application to view the output