Hide Menu Item in Layout

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?
Stacey FontenotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zakaria AcharkiAnalyst DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lenamtlCommented:
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 ....
Stacey FontenotAuthor Commented:
is your syntax correct? I'm getting an token error with poor format.
Determine the Perfect Price for Your IT Services

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

Zakaria AcharkiAnalyst DeveloperCommented:
Could you please take a screenshot and show us the error message?
Stacey FontenotAuthor Commented:
Great work.
Zakaria AcharkiAnalyst DeveloperCommented:
Glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.