I work on mvc 5 project.
On my _Layout.cshtml
I have html element named id="topBar"
this element displayed in each page.
But I have page named About.cshtml I don't want this page to display only element id="topBar"
all other html elements in _Layout.cshtml
I want them to be displayed normal in About.cshtml
.
Any idea how can achieve described appearance above?
One simple way of achieving this (without javascript), is to set a value in ViewBag
inside your about Action
and use it in layout like this:
_Layout.cshtml
@if (ViewBag.ShowTopBar ?? false)
{
<div id="topBar">
</div>
}
Action inside your Controller:
public class MyAwesomeController : Controller
{
public ActionResult About()
{
ViewBag.ShowTopBar = true;
return View();
}
}
There are two options:
body
elementYou could add an ID
to the body of each of your pages that contains for example the Controller and Action name and then add a CSS rule to hide the topBar element on the about page.
First, let's create two string variables for the controller and action name. Do this in your _Layout.cshtml:
@{
string controller = ViewContext.RouteData.Values["controller"].ToString();
string action = ViewContext.RouteData.Values["action"].ToString();
}
Next, add the ID to the body:
<body id="@controller-@action">
In your css, you can now add a rule to hide the topbar in the about page (assuming the controller is called HomeController):
#Home-About #topBar {display:none;}
You could also use an extension method to get the controller and action name. For example something like this.
You could add a Javascript on your about page and use jQuery to hide the topbar (I assume jQuery is already loaded):
<script>
$("#topBar").hide();
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With