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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • Docker Base Images Demystified: A Practical Guide
  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.1K 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.

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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!