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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Gunnar Peipman user avatar by
Gunnar Peipman
·
Apr. 04, 19 · Tutorial
Like (1)
Save
Tweet
Share
12.99K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • Top Five Tools for AI-based Test Automation
  • GPT-3 Playground: The AI That Can Write for You
  • Implementing Adaptive Concurrency Limits

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: