31

在我的视图中,我有一些管理链接,我想根据用户角色隐藏和显示如何在视图中执行此操作,例如

<%= if(CHECK IF USER ROLE ADMIN) { %>
        <div class="tools">
            <ul>
                <li class="edit"><%= Html.ActionLink("Edit", "Edit", new { id = Model.storyId }) %></li>
                <li class="delete"><%= Html.ActionLink("Delete", "Delete", new { id = Model.storyId }) %></li>
            </ul>
        </div>
<%= } %>
4

3 回答 3

63
@if (this.User.IsInRole("Administrator"))
{

}
于 2014-02-12T21:00:29.193 回答
27
<% if (Page.User.IsInRole("Admin")){ %>

<%}%>

However this is a terrible idea in my opinion. It is better to let the ViewData or Model represent what the view is to display, and the view can simply check the view data. A controller base class or an action filter can make repetitive use of this very simple and allow the code to exist in one place.

于 2011-01-06T00:59:02.703 回答
0

我同意大多数其他人的观点,即如果您愿意,这些数据应该由控制器或其他业务服务“预先确定”提供,而 View 只是尽可能多地使用 html 标记和语言控制结构来“充实page”使用其他典型的网页构建好东西,如 jquery、css 等。

于 2011-01-06T03:44:06.517 回答