views:

928

answers:

1

Hi,

i want to show/hide certain parts of a View based on Authentication-status or Roles. For my controller actions I have extended ActionFilterAttribute so I can attribute certain Actions.

<RequiresRole(Role:="Admin")> _
Function Action() as ActionResult
    Return View()
End Function

Is there a similar way (attributing) which I can use in the Views? (so not like this: http://stackoverflow.com/questions/409213/asp-net-mvc-roles-views)

+2  A: 

You can access the user's logged-in roles from the view like this:

<% if (Page.User.IsInRole("Admin")) { %>
        <td>
          <%= Html.DeleteButton("delete", model.ID) %>
        </td>
<% } %>

and maybe your extension method with something like:

public static string DeleteButton(this HtmlHelper html, 
    string linkText, int id)
{
    return html.RouteLink(linkText,
     new { ID = id, action = "Delete" },
     new { onclick = "$.delete(this.href, deleteCompleted()); return false;" });
}

Obviously, I'm using JavaScript to perform an HTTP DELETE to my controller action, to prevent page crawlers from accidentally deleting data from getting my pages. In my case I'm extending JQuery with a delete() method to supplement the HTTP verb.

Kris
Ok, so using attribute-based view rendering is probably not possible. I should go with If statements...Thx
Ropstah