Ognjen
Ognjen

Reputation: 1195

Create menu dynamically based on user's role

How do I create a menu in a ASP.NET MVC2 Master Page, dynamically based on the current user's "role"?

Upvotes: 1

Views: 2533

Answers (3)

Joe Phillips
Joe Phillips

Reputation: 51200

If you are using the sitemap file to generate menus then you can probably do it in there. If not, then it depends.

Upvotes: 0

Aaronaught
Aaronaught

Reputation: 122684

The simplest and most straightforward way would be to simply add an if statement in the view markup:

<% if (Page.User.IsInRole("Admin")) { %>
   <%= Html.ActionLink("Admin Tools Index", "Index", "Admin") %>
   <%= Html.ActionLink("Admin Dashboard", "Dashboard", "Admin") %>
<% } %>

Or, you can separate out several items pertaining to a specific role into a partial view:

<% if (Page.User.IsInRole("Admin")) { %>
   <% Html.RenderPartial("AdminMenu"); %>
<% } %>

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273824

I'm not sure about MVC but in 'normal' ASP.NET it is possible to select a MasterPage at runtime.

Upvotes: 1

Related Questions