Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

How do I display a different view based on membership role?

I am brand new to MVC, and i'm jumping in head first. I am creating a time and attendance application. I want the user to simply log in and see the user interface for clocking in. However, for salaried employees I want to provide a different interface. My thought on this is to simply offer two views, but am unsure of the "customary" method of doing this.

For now i'm using the AccountController to handle authentication, and using standard Authorization attributes.

Should I simply have my home controller's index action check the role and return a different view? Or should I somehow route users to different actions based on their membership role?

Upvotes: 1

Views: 766

Answers (1)

rboarman
rboarman

Reputation: 8214

One way to do it is to setup Roles and then use code like this in your views:

<%  
    if (User.IsInRole("AdminRole") 
        Html.RenderPartial("AdminView");  
    else if (User.IsInRole("SalariedRole") 
        Html.RenderPartial("SalaryView");  
%>

You can also do it with a filter:

http://weblogs.asp.net/fredriknormen/archive/2008/03/12/asp-net-mvc-framework-2-interception-and-creating-a-role-action-filter.aspx

Upvotes: 1

Related Questions