In general, a Partial View is like a web user control in ASP.Net applications. Partial Views and User Controls serve the same purpose.

Uses of Partial Views

Rendering Partial View: We can render a partial view using one of the following 4:

  1. Html.Partial
  2. Html.RenderPartial
  3. Html.Action
  4. Html.RenderAction

Now in my example, I will add a partial view. For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View.

Add View
Image 1

Give a name to your View and check Create As a partial View as in the following:

create as a partial view
Image 2

Now I am writing a line of text in this Partial View:

Partial View
Image 3

Now for the Home controller, here I add a new action ShowMyPartialView(). So after it my Home Controller will look such as below:

home controller
Image 4

Now for the View -> Home -> Index.cshtml.

Here I am rendering a Partial View using 4 types, so the index.cshtml will be like the following:

  1. @{
  2. ViewBag.Title = "Home Page";
  3. }
  4. @section featured {
  5. <section class="featured">
  6. <div class="content-wrapper">
  7. <hgroup class="title">
  8. <h1>@ViewBag.Title.</h1>
  9. <h2>@ViewBag.Message</h2>
  10. </hgroup>
  11. </div>
  12. </section>
  13. }
  14. <h3>Rendering Partial View:</h3>
  15. <ol class="round">
  16. <li class="one">
  17. <h5>By Using Html.RenderPartial #</h5>
  18. @{Html.RenderPartial("MyPartial"); }
  19. </li>
  20. <li class="two">
  21. <h5>By Using Html.Partial #</h5>
  22. @Html.Partial("MyPartial")
  23. </li>
  24. <li class="three">
  25. <h5>By Using Html.RenderAction #</h5>
  26. @{Html.RenderAction("ShowMyPartialView", "Home");}
  27. </li>
  28. <li class="four">
  29. <h5>By Using Html.Action #</h5>
  30. @Html.Action("ShowMyPartialView");}
  31. </li>
  32. </ol
Now run the application:

Partial View demo
Image 5