Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:37:59+00:00 2026-05-25T17:37:59+00:00

So with layouts in MVC3 lets say I want to be able to specify

  • 0

So with layouts in MVC3 lets say I want to be able to specify on a page level if a particular section is displayed, what is the best way to go about it. Consider the following page:

@{
     ViewBag.Title = "...";
     Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}

@section LetsBeFriends {

}

@section Header {
    ....
}

@section Body {
    ....
}

For the LetsBeFriends section to be conditional I have implemented the layout like this:

@{
        if (IsSectionDefined("LetsBeFriends"))
        {
            @RenderSection("LetsBeFriends")
            @Html.Partial("_LetsBeFriends")
        }
}

@RenderSection("Body")

This seems hacky because LetsBeFriends will always be an empty section, its just a condition to decide whether to render the partial. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:38:00+00:00Added an answer on May 25, 2026 at 5:38 pm

    Why not use the ViewBag? In your page:

    @if (friendsCondition)
    {
        ViewBag.LetsBeFriends = true;
    }
    

    Then, in _Layout.cshtml:

    @if (Viewbag.LetsBeFriends)
    {
        @Html.Partial("_LetsBeFriends")
    }
    

    However, it is even better to set this in the controller action, rather than the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please advice the best way to organize editing my Restaurant entity in MVC3. I
I'm using ASP.NET MVC3 with razor engine.I want to apply css class on body
I am working on an ASP.NET MVC3 app. I was reading about good Javascript
Could i include a single page .cshtml in a MVC3 app (without controller)? I
I Have a little problem with WebGrid control for Asp.Net MVC3. What I want
I've got a question about layout pages in MVC3: Is it possible to use
I am working on project using asp.net mvc3 C# . I want to change
I have a Razor layout MVC3 page as below: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML
On my MVC3 _Layout.cshtml page I show a banner image like this <body> <div
I created an asp mvc3 project, I want to have a different _Layout.cshtml depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.