views:

32

answers:

2

I have an MVC view that changes a few small UI elements based on the user's role. Currently it modifies the output using a few if() statements using a couple boolean values I sent to the view through the viewmodel. However, it doesn't seem like a very good approach, so what I'd like to do is move this all into either an HtmlHelper class or the controller itself. Here's the current code:

<% if ( Model.IsAdmin ) { %> <td> <% } %>  <%--Opening tag for admin options--%>
<% if ( Model.IsAdmin ) { %> <%:Html.ActionLink("Delete", "Delete", new { id = Id})%> <% } %> <%--Admin options--%>
<% if ( Model.IsAdmin ) { %> </td> <% } %> <%--Closing tag for admin options--%>

There's a seperate spot where I show/hide the create new link as well

<% if ( Model.IsEditor || Model.IsAdmin ) { %>
    <%:Html.ActionLink("Create New", "Create") %>
<% } %>

There might be a couple other of these situations as well.

So any HtmlHelper I would build would potentially need a couple overloads for the different cases, so it would probably need to be pretty flexible. I've just been going around and around in my head on the best approach to this, and it seems like a common problem that someone else would probably have come up with already...

Thanks!

A: 

Either use a partial views in your views or create different views for each role and render the view according to the role. you can also check RenderAction.

Adeel
A: 

Looks like this solution would fit your needs:

Role-Based Content asp.net mvc

Bryce Fischer
Thanks Bryce, that's what I was looking for. Although considering that I might potentially have a lot of these little checks, I'm thinking a partial view might be the best approach.
morganpdx