DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Trending

  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  1. DZone
  2. Coding
  3. Frameworks
  4. Conditionally Include Partial View in ASP.NET Core

Conditionally Include Partial View in ASP.NET Core

A developer demonstrates how to use view injection to create a left-hand menu that will only appear in our application when it is needed.

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Apr. 04, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

I have an ASP.NET Core web application where I need to include a partial view only if it exists. The application uses areas and some areas may have their specific side menu. This blog post shows two ways to conditionally include a partial view in an ASP.NET Core layout page.

My goal is to do it the easiest way possible by using out-of-the-box things and making sure I don't create an ugly looking mess. I wanted to avoid all kinds of additional classes to wrap dirty secrets away from controllers and views.

As it turns out, it is actually an easy thing to do thanks to view injection in ASP.NET Core. Here's how to do it:

  1. Inject an instance of ICompositeViewEngine into the layout page.
  2. If a partial view exists it will generate mark-up where it's included.
  3. If a partial view doesn't exist then it will be left out.

I added the following line of code to the very beginning of the layout page.

@inject ICompositeViewEngine Engine

Then I replaced the RenderBody() method in the layout page with the following block of Razor code.

<div class="row">
    @if (Engine.FindView(ViewContext, "_LeftMenu", isMainPage: false).Success)
    {
        <div class="col-md-3">
            <partial name="_LeftMenu" />
        </div>
        <div class="col-md-9">
            @RenderBody()
        </div>
    }
    else
    {
        <div class="col-md-12">
            @RenderBody()
        </div>
    }
</div>

In my case, there was the need for a different mark-up if a partial view is present. Without this need, things get easier. There's no need to inject anything to the layout page and we can use the partial tag helper optional-parameter.

Wrapping Up

Before inventing custom code components to achieve something on ASP.NET Core, it is a good idea to see what the framework provides out-of-the-box. This blog post introduced how to conditionally include a partial view to an ASP.NET Core layout page. With view injection, we got a view engine instance to the layout page to check if a partial view exists. Based on this we generated markup to show the page with or without the left menu.

ASP.NET ASP.NET Core

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook