开发者

Better approach to role-based UI changes?

开发者 https://www.devze.com 2023-01-18 16:55 出处:网络
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

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

精彩评论

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

关注公众号