1

I'm new to ASP.NET MVC and am wondering what I should be doing if I my _Layout.cshtml has an element

<div class="navbar navbar-inverse navbar-fixed-top">
   ...
</div>

that I don't want generated for a particular page with controller SomePageController.cs and view SomePage.cshtml. Do I just wrap that piece of HTML in an if statement like

@if(not on SomePage) {
    <div class="navbar navbar-inverse navbar-fixed-top">
       ...
    </div>
}

or is there a more proper way?

user4905335
  • 351
  • 2
  • 11

4 Answers4

2

I usually use a magic string in ViewBag for this. I set it in my controller (or you can do it in the top of the view, if you wish).

ViewBag.HideNavBar = true;

in the _Layout:

@if(ViewBag.HideNavBar == null) {
    <div class="navbar navbar-inverse navbar-fixed-top">
       ...
    </div>
}

I use this strategy to pass options into the layout (such as datepicker options) all the time.

Brad C
  • 2,619
  • 19
  • 32
1

Give the single page that doesn't need that <div> a different Layout:

@model Foo
@{
    Layout = "~/Views/Shared/_LayoutWithoutNavigation.cshtml";
}

<!-- rest of HTML --> 
Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
1

you can do it multiple way

1: Simple trick.. just use If condition and get the route name from Query string and match it with your route .

2: use two different layouts. one with that HTML part and one without HTML part. and when creating view just specify the layout without HTML.

3: Use ViewBag / tempData

Approaches:

First:

if(Request.QueryString["routeName"] != "MyRoute" )
{
   //Render HTML Part
}

second:

@{
 layout = "~/shared/LayoutWithoutHTML.cshtml";
}

Third

in your Controller where you want to hide HTML

viewBag.HTMLCheck = true;

In your View

if(viewBag.HTMLCheck != true)
{
  // Html Part
}
Zohaib Waqar
  • 1,074
  • 10
  • 18
0

Your solution seems like an appropriate way to go about it - ideally though, if it's not going to be on every page, the pages without that section should use a different Layout.

Donald
  • 960
  • 10
  • 19