Reputation: 12988
In my MVC 4 project, i have 2 menus, one top fixed menu and a side menu. The top menu is always visible, even when the user isnt logged. But the side menu is only visible when the user is logged. But if the user has a admin role he is able to acces the "Create User" view, and i wish the side menu would not be visible in that speciefic view. How can i do that?
Here´s the side menu part in my layout view:
@if (Request.IsAuthenticated)
{
<div class="row">
<div class="col-md-3">
<p class="lead">Comparações</p>
<div class="list-group">
<a href="#" class="list-group-item">Item 1</a>
<a href="#" class="list-group-item">Item 2</a>
</div>
</div>
</div>
}
Upvotes: 0
Views: 1817
Reputation: 4225
I used css - you can make this into "HideMenu.cshtml" and call @RenderPage("HideMenu.cshtml")
in any cshtml file where you don't want to see the menu.
<style>
body {
padding-top : 0px;
}
.navbar {
display: none;
}
</style>
In this css, "navbar" is the class of the menu item I want to hide.
Use F12 from your browser to find the class name of the element you want to hide.
Upvotes: 2
Reputation: 1469
Try:
@if (Request.IsAuthenticated && !HttpContext.Current.User.IsInRole("Admin"))
{
<div class="row">
<div class="col-md-3">
<p class="lead">Comparações</p>
<div class="list-group">
<a href="#" class="list-group-item">Item 1</a>
<a href="#" class="list-group-item">Item 2</a>
</div>
</div>
</div>
}
Upvotes: 0
Reputation: 11301
I use ViewBag to set a flag from the controller. This flag determines whether to do something, but make sure that missing value is also valid and it indicates default action. In that case, only the actions that are affected by the additional part of the layout are free to set the ViewBag item to specific value.
For example, I have a sign in form in layout. But sign in should not be rendered when user visits sign up page, obviously. So, there is a HideSignInForm with default False (and missing equals False). In _Layout.cshtml this is covered by this piece of code:
bool hideSignInForm = false;
if (ViewBag.HideSignInForm != null)
{
hideSignInForm = (bool)ViewBag.HideSignInForm;
}
Later on in the _Layout.cshtml, I use the flag normally:
if (!hideSignInForm)
{
<div id="signInUser">
...
</div>
}
Upvotes: 0