Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - menu for different roles

I am writing my first MVC 3 aplication (in ASP.NET) and I don't know how I can(should) display the menu for different users.

My app is created as MVC3 Web Application and the menu look like this:

<div id="menucontainer">
            <ul id="menu">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("Info", "Info", "Home")</li>
            </ul>
</div>

I created two types of roles: user and admin. Now, I want to show another links for user(Projects, Profile) and for admin(Manage Projects, Manage Accounts, Manage news).

How I should do that?

like image 914
Jacob Jedryszek Avatar asked Feb 07 '11 01:02

Jacob Jedryszek


1 Answers

I found solution:

<div id="menucontainer">
            <ul id="menu">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("Info", "Info", "Home")</li>
                @if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole
( "user" ) ) {
                     <li>Projects link</li>
                     <li>Profile link</li>
                }
                @if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole
( "admin" ) ) {
                     <li>Manage Projects link</li>
                     <li>Manage Accounts link</li>
                }
            </ul>
</div>
like image 145
Jacob Jedryszek Avatar answered Oct 17 '22 18:10

Jacob Jedryszek