Hide Menu Item in Layout

Stacey Fontenot
Stacey Fontenot used Ask the Experts™
on
I have an MVC C# Web application. I need a way to check a cookie name ="User_Role", if it = "UR500" then hide a menu object which is included in the layout.cshtml. Where would I place this script?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Developer
Distinguished Expert 2018
Commented:
You could add the script inside the Layout.cshtml view, you may use :

@Scripts.Render("~/Scripts/Custom/checkCookies.js")

Open in new window

Since you're using jQuery you could use the jquery.cookie library by including it first :

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

Open in new window


Then the script will look like :

<script>
$(function(){
  if( $.cookie('User_Role') == "UR500"){
      $('your_menu_selector').hide(); //Or .remove()
  }
});
</script>

Open in new window

Hi,

I don't recommend to use JS for this, this is not secure.
You should use PHP or server side code to check the role and display / hide the menu item like you would do in your page for the content.
if user_role = something show else hide ....

Author

Commented:
is your syntax correct? I'm getting an token error with poor format.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
Could you please take a screenshot and show us the error message?

Author

Commented:
Great work.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
Glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial