tags:

views:

124

answers:

1

i have a lot of code like this in my controllers:

    private void PopulateAdminStatus()
    {
        if (User.IsInRole("Administrator"))
        {
            ViewData["isAdmin"] = true;
        }
        else
        {
            ViewData["isAdmin"] = false;
        }
    }

the only reason i need to do this is to have my Views check admin status (as it shows different things on the view)

is there any cleaner way to have the Views have access to the User object without going through ViewData?

+3  A: 

you can do this in your view/partial views

 <% if(Page.User.IsInRole("Administrator")){%>

Like RobCon says "If there’s an IF, make a Helper", so you could move your role/admin check into a helper and call the helper in your view any time you need the check.

KP