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
Newsletter
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

  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Apr. 04, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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