ASP.NET MVC Check role inside view

£可爱£侵袭症+ 提交于 2019-12-18 12:13:21

问题


In my View I have some admin links that I would like to hide and show based on user role how can do this inside the view e.g.

<%= 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>
<%= } %>

回答1:


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

}



回答2:


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




回答3:


I agree with most others that this data should be provided "pre-determined", if you will, by the controller or other business services whereas the View simply uses, as much as possible, html markup and language control structures to "flesh out the page" using other typical web page building goodies such as jquery, css, etc. etc.



来源:https://stackoverflow.com/questions/4610749/asp-net-mvc-check-role-inside-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!