How To Use Partial Actions And Partial Views In ASP.NET MVC

As we already know that Partial views are the small views that we can render in different views. We use partial views to breakdown the large views, we also use the partial views for the reusability purpose. Let’s take an example of Partial Views.

I was working in a project, during working I personally feel that sometimes most of the developers confuse about how to use partial views and partial actions to render the dynamic data. So I just complete my task and write the article to help others. As we already know, Partial Views are the small Views that we can render in different Views. We use partial Views to break down the large Views. We also use partial Views for reusability purposes. Let’s look at an example of Partial Views.

Most of the time developers just want to use Partial Views in 2 scenarios.

  • For Static Purposes
    Where they just want to show some content in the view which is not dynamic at all.
  • For Dynamic Purpose
    Where he wants to populate the data from the model and pass it into the Partial View through a controller action.

Let’s discuss both of these scenarios and how we can use the Partial Views and Partial Actions.

  • Create a sample Empty MVC Application.

Static Scenario

@Html.Partial

  • Open the _layout.cshtml page, here we’ve complete theme HTML page including navbar code as well. Let’s break down the code by making sub views (partial page), So
  • Add ‘_Navbar.cshtml’ a partial page into Shared View.

    ASP.NET

    It is the convention that we make the partial page names starting with an underscore like we do here.
  • Now, cut and paste the navbar div from ‘_layout.cshtml’ to ‘_Navbar.cshtml’.
    1. <div class="navbar navbar-inverse navbar-fixed-top">  
    2.     <div class="container">  
    3.         <div class="navbar-header">  
    4.             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
    5.                 <span class="icon-bar"></span>  
    6.                 <span class="icon-bar"></span>  
    7.                 <span class="icon-bar"></span>  
    8.             </button>  
    9.             @Html.ActionLink("Application name""Index""Home"new { area = "" }, new { @class = "navbar-brand" })  
    10.         </div>  
    11.         <div class="navbar-collapse collapse">  
    12.             <ul class="nav navbar-nav">  
    13.                 <li>@Html.ActionLink("Home""Index""Home")</li>  
    14.                 <li>@Html.ActionLink("About""About""Home")</li>  
    15.                 <li>@Html.ActionLink("Contact""Contact""Home")</li>  
    16.             </ul>  
    17.             @Html.Partial("_LoginPartial")  
    18.         </div>  
    19.     </div>  
    20. </div>  
  • Now, we explicitly need to render the partial page in the main (_layout.cshtml) page.
    1. <body>  
    2.     @Html.RenderPartial("_Navbar")  
    3.     <div class="container body-content">  
    4.         @RenderBody()  
    5.         <hr />  
    6.         <footer>  
    7.             <p>© @DateTime.Now.Year - My ASP.NET Application</p>  
    8.         </footer>  
    9.     </div>  
    10.   
    11.     @Scripts.Render("~/bundles/jquery")  
    12.     @Scripts.Render("~/bundles/bootstrap")  
    13.     @RenderSection("scripts", required: false)  
    14. </body>  
  • We can also pass the model to the partial page as well but it is optional, as we’re splitting the layout page and make another partial page from the layout page. So we don’t have any need to pass the object to this (‘_Navbar.cshtml’).

Navbar is all about to show the navbar on the main home page.

@Html.RenderPartial

And if we want to use Html.RenderPartial HTML helper in the same scenario, then we need to enclose the HTML helper in Razor block of code. Like we do here,

  1. @{  
  2.         Html.RenderPartial("_Navbar");  
  3. }  

It works the same. The difference is @Html.Partial renders the partial view in a string whereas @Html.RenderPartial directly writes in the response stream of the view instead of returning the string. And obviously, @Html.RenderPartial is more efficient than a previous one. But most of the time developer just know about the keyword partial, so instead of knowing about RenderPartial HTML helper he just go ahead with Partial() helper.

Dynamic Scenario

Now, let’s suppose you want to show the data on the master page or _Layout page. And you want to show the data from the database, then how will you do it? Most of the time developers face this situation.

Let me share the solution with you.

So here, we use these Html Helpers

  • Html.Action()
  • Html.RenderAction()

@Html.Action

Here, we just need to call the action with its action name in the view. Like I’ve done it here.

ASP.NET

As you can see I’m calling 2 actions (Message and ContactFooter) in my _Layout.cshtml page.

And now come back to the actions which we’re calling here. Here I’ve written the logic of these actions.

  1. [ChildActionOnly]  
  2. public ActionResult Message()  
  3. {  
  4.     IEnumerable<News> news;  
  5.     using (ApplicationDbContext context = new ApplicationDbContext())  
  6.     {  
  7.         news = context.News.Where(x => x.State).ToList();  
  8.     }  
  9.     return PartialView("_News", news);  
  10. }  
  11.    
  12. [ChildActionOnly]  
  13. public ActionResult ContactFooter()  
  14. {  
  15.     Contact contact;  
  16.     using (ApplicationDbContext context = new ApplicationDbContext())  
  17.     {  
  18.         contact = context.Contacts.SingleOrDefault();  
  19.     }  
  20.     return PartialView("_Contact", contact);  
  21. }  

This is how we write the partial actions. It is necessary to attribute these actions with [ChildActionOnly] attribute. Don’t make async calls to Db in partial actions, it causes the errors. You can see how to call the Partial views and pass the model to the views.

And more for demo purposes here is the partial view. Because most of the time, People also confuse on how to code for partial Views, it is really exactly the same as normal Views.

  1. @model IEnumerable<TBRD.Models.News>  
  2.    
  3. <ul class="marquee">  
  4.     @foreach (var message in Model)  
  5.     {  
  6.         <li>  
  7.             <i class="fa fa-angle-double-left"></i>  
  8.             <a href="@message.Link" target="_blank">  
  9.                 @message.Text  
  10.             </a>  
  11.         </li>  
  12.     }  
  13. </ul>  

Simply this is the code of Partial View (_News.cshtml) in my case.

@Html.RenderAction

And if you want to go with this action then you just need to use the Html helper in razor block like we do above with Html.PartialView()

  1. @{  
  2.     Html.Action("ContactFooter""Home");  
  3. }  

Conclusion

Today, we’ve seen how we can use different kinds of Partial Views, how they work with each other, how we can show the dynamic data on the master page or _layout page with the help of partial actions. Don’t use the asynchronous calls to the DB in partial action, otherwise, you’ll bother a lot about the wrong things. You can just remember only 2 HTML helpers for your ease: @Html.Action to call the partial action and @Html.Partial to render the partial View. It will make your life easier.