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 9122355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:05:00+00:00 2026-06-17T06:05:00+00:00

In my _Layout.cshtml I have the following code which will not show the buttons

  • 0

In my _Layout.cshtml I have the following code which will not show the buttons if the user’s role is UserType1

     @if (!(HttpContext.Current.User.IsInRole("UserType1")))
     { 
       <li><a href="../St/Index">Static</a> </li>
       <li><a href="../St/Clients">Clients</a> </li>
     }

I was wondering if there is a better or best practice way of doing this. Please note that I am putting this code in my _Layout.cshtml file.

  • 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-06-17T06:05:02+00:00Added an answer on June 17, 2026 at 6:05 am

    This seems fine. Some people (like me) might prefer to bring this a step further and wrap this check in a custom HTML helper though in order to be able to unit test the condition:

    @if (Html.ShouldShowButtons())
    { 
        <li><a href="../St/Index">Static</a> </li>
        <li><a href="../St/Clients">Clients</a> </li>
    }
    

    and the implementation of the helper:

    public static class HtmlExtensions
    {
        public static bool ShouldShowButtons(this HtmlHelper html)
        {
            return html.ViewContext.HttpContext.User.IsInRole("UserType1");
        }
    }
    

    As an alternative you could implement more complex logic using child actions:

    @{Html.RenderAction("MenuButtons", "Menu");}
    

    You would then have a corresponding controller action which is checking whether the user is in the required role and render a partial view with the markup.

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

Sidebar

Related Questions

I have following code snipped in _Layout.cshtml <div id=sub-navig-container> @RenderSection(subNavig) </div> <div id=text-content> @RenderBody()
I have the following code in my _Layout: @if (ViewBag.PageFormat == Footer Buttons) {
I have the following view: @model MyModel1 @{ ViewBag.Title = Index; Layout = ~/Views/Shared/_Layout.cshtml;
I have a _Layout.cshtml page which I've used to include all master page styling.
I have the following in my _Layout.cshtml: <title>@ViewData[PageTitle]</title> I then have a Child Action
I have a _ViewStart.cshtml in my main view folder with the following code. @{
In _Layout.cshtml I have the following defined in the tag: @RenderSection(JavaScript, required: false); The
In my _Layout.cshtml file I have the following lines <!--[if IE 7]> <link rel=stylesheet
I have a following project structure: _ViewStart.cshtml: @{ Layout = ~/Views/Shared/_Layout.cshtml; } _Red.cshtml: @{
I have _Layout.cshtml defined for my mvc application, which is shown below: @inherits System.Web.Mvc.WebViewPage

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.