ASP.NET MVC - show hide user controls depending on role

In an MVC2 (c#) app I checking for role in master page and displaying menu items like this

<% if (Model.IsUserAdmin)
                       { %>
                    <li><a id="lnk" runat="server" href="/Admin/">Admin Area</a></li>

and so on.

There are 3 roles in the app  SuperUser, Admin, User

Dependant who is logged into the app I want to show a logged in user's section (user control) that shows
different data for each role.

I am thinking of maybe 3 user controls one for each role and show/hide depending on role?

Or just one user control and show/hide dependant on role.

The scenario is

User    -  display logged in name
Admin - display logged in name AND  dropdown list of things
SuperUser  - display logged in name AND  dropdown list of things AND another dropdown list of things


ToString1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Carl TawnSystems and Integration DeveloperCommented:
Well for starters it's MVC so there is no such thing as a user control :)

I think your best option would be to render a PartialView and let it decide what to show based on your model. That way the logic is encapsulated in your PartialView rather than the main View, and then you only have to worry about telling it where to draw.
0
ToString1Author Commented:
Thanks

Could you give a quick  example please
0
Carl TawnSystems and Integration DeveloperCommented:
Well, i'm going to make a bit of a sweeping assumption that you know how to add a PartialView to your project, and that you know (as you would appear to based on your original sample) that you know how to define the logic to control wat the PartialView displays.

To actually display the partial view and pass it your Model, you would use something like the following in your main view:
<%: Html.RenderPartial("UserAdmin", Model);

Open in new window

Which assumes your partial view is named "UserAdmin" and that it is strongly typed to the same Model as your main view.
0
ToString1Author Commented:
OK thanks

So inside the partial view I would show/hide divs based on role?   Or would these be panels?

Just a quick example of showing hiding dropdowns would be good

0
Carl TawnSystems and Integration DeveloperCommented:
Very simply you would do something like the following, which uses a hard-coded list of drop down options:
<% if (Model.IsAdmin) { %>

    <%: Html.DropDownList("ADropDown", new List<SelectListItem> { new SelectListItem { Text = "One" }, new SelectListItem { Text = "Two" }, new SelectListItem { Text = "Three" } }) %>

<% } %>

Open in new window

0

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
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
Web Frameworks

From novice to tech pro — start learning today.