A partial view is same as user control in ASP.NET MVC that is used for code reusability. Partial views help us to reduce code duplication. Hence, partial views can be used for views like Header and Footer views. In web form application, we use user control for creating reusable elements. The same way in MVC, we will use a partial view.
- <div> @Html.Partial("_status") </div>
- div> @{Html.RenderPartial("_status");} </div>
- <div> @{Html.RenderAction("_MyView","Profile");} </div>
- <script type="text/jscript">
- $('#satyapopup').load('/shared/_MyView’);
- </script>
Note
| Html.RenderPartial | Html.RenderAction | Html.Partial | Html.Action |
| This method returns void. | We need to create a child action for rendering the partial view. | Renders the partial view as an HTML-encoded string. | Renders the partial view as an HtmlString . |
| The method result will be directly written to the HTTP response stream which means it used the same TextWriter object as used in the current webpage/template. No need to create any action.
|
This method result will be directly written to the HTTP response stream which means it used the same TextWriter object as used in the current webpage/template. RenderAction method is useful when the displaying data in the partial view is independent from corresponding view model. | Result can be stored in a variable, since it returns string type value. Simple to use and no need to create any action. | We need to create a child action for rendering the partial view. This method result can be stored in a variable, since it returns string type value. |
| This method is faster than Partial method since its result is directly written to the response stream. RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model. | This method is the best choice when you want to cache a partial view. This method is faster than Action method since its result is directly written to the HTTP response stream which makes it fast. | Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model. | Action method is useful when the displaying data in the partial view is independent from corresponding view model. |

Step-2
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MVCPartialView.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult ViewWithPartial()
- {
- return View();
- }
- public ActionResult SatyaAcademicYear()
- {
- using (SatyaprakashEntities dc = new SatyaprakashEntities())
- {
- var v = dc.AcademicYears.OrderByDescending(a => a.AcademicYear1).ToList();
- return PartialView("_AcademicYear", v);
- }
- }
- public ActionResult ViewWithPartialtwo()
- {
- return View();
- }
- }
- }
- return PartialView("_AcademicYear", v);
- @model IEnumerable<MVCPartialView.AcademicYear>
- @{
- ViewBag.Title = "_AcademicYear";
- }
- <h2>Academic Year List</h2>
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- tr:nth-child(even) {
- background-color: #dddddd;
- }
- </style>
- <table border="1" cellpadding="4" cellspacing="4">
- <tr>
- <th style="background-color: Yellow;color: blue">
- AcademicYearID
- </th>
- <th style="background-color: Yellow;color: blue">
- AcademicYearName
- </th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.AcademicYear1)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.AcademicYearID)
- </td>
- </tr>
- }
- </table>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.AcademicYear1)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.AcademicYearID)
- </td>
- </tr>
- }
- @{
- ViewBag.Title = "ViewWithPartial";
- }
- <fieldset>
- <legend style="font-family:Arial Black;color:blue">Normal View Content and Partial View Content</legend>
- <table>
- <tr>
- <td>
- <b>Normal View Content One</b>
- <p>
- <a href="http://www.c-sharpcorner.com/members/satyaprakash-samantaray">
- <img src="http://www.c-sharpcorner.com/members/satyaprakash-samantaray/flair.png" width="260" height="58" alt="profile for Satyaprakash C# Corner - A Social Community of Developers and Programmers" title="profile for Satyaprakash at C# Corner - A Social Community of Developers and Programmers" />
- </a>
- <br />
- <br />
- I'm a C# Corner MVP , Software Developer, Designer.
- I enjoy developing Microsoft .NET technology stuffs.
- I am passionate about Microsoft .NET technology and like to contribute my skills to the .NET developers community.
- My Contribution will let me step towards to be a future entrepreneur.
- </p>
- </td>
- <td width="400px">
- <b>Partial View Content</b>
- @{Html.RenderAction("SatyaAcademicYear", "Home");}
- </td>
- </tr>
- </table>
- </fieldset>
- <footer>
- <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@
- </footer>
- <td width="400px">
- <b>Partial View Content</b>
- @{Html.RenderAction("SatyaAcademicYear", "Home");}
- </td>
- @{
- ViewBag.Title = "ViewWithPartialTwo";
- }
- <fieldset>
- <legend style="font-family:Arial Black;color:blue">Normal View Content and Partial View Content</legend>
- <table>
- <tr>
- <td>
- <b>Normal View Content Two</b>
- <p>
- <a href="http://www.c-sharpcorner.com/members/satyaprakash-samantaray">
- <img src="http://www.c-sharpcorner.com/members/satyaprakash-samantaray/flair.png?theme=dark" width="260" height="58" alt="profile for Satyaprakash C# Corner - A Social Community of Developers and Programmers" title="profile for Satyaprakash at C# Corner - A Social Community of Developers and Programmers" />
- </a>
- <br />
- <br />
- I'm a C# Corner MVP , Software Developer, Designer.
- I enjoy developing Microsoft .NET technology stuffs.
- I am passionate about Microsoft .NET technology and like to contribute my skills to the .NET developers community.
- My Contribution will let me step towards to be a future entrepreneur.
- </p>
- </td>
- <td width="400px">
- <b>Partial View Content</b>
- @{Html.RenderAction("SatyaAcademicYear", "Home");}
- </td>
- </tr>
- </table>
- </fieldset>
- <footer>
- <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@
- </footer>
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "ViewWithPartial", id = UrlParameter.Optional }
- What is partial view in MVC.
- Implement partial view in multiple pages in MVC.
- Avoid Duplication of code using Partial View.
- Partial View using Entity Framework.


Tridip BhattacharjeePosted Dec 10, 2017, 10:19 PM EST
If we load partial view $('#satyapopup').load('/shared/_MyView’); this way then how view will get model data to populate its controls?