开发者

Better approach to role-based UI changes?

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.Is开发者_开发知识库Admin ) { %> </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!


Looks like this solution would fit your needs:

Role-Based Content asp.net mvc


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜