Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Check role inside view

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>
<%= } %>
like image 682
Cameron Avatar asked Jan 06 '11 00:01

Cameron


2 Answers

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

}
like image 64
Pažout Avatar answered Oct 19 '22 23:10

Pažout


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

like image 39
CRice Avatar answered Oct 20 '22 01:10

CRice