How can I create a view that has different displays according to the role the user is in?

后端 未结 8 1374
南方客
南方客 2020-12-04 11:22

I want to create a view that has different displays according to the role the user is in.

Should I create a different view for different roles or should I check the

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 12:18

    I like to have full control over this in the view, and I find that:

    <% if (User.IsInRole("Super User")) { %>
        

    Hello world!

    <% } %>

    Works for most scenarios. It also allows you to easily do conditional formatting for other roles, e.g "Content Manager", "Registered", etc.

    I do like Todd Smith's answer, because you might change the name of the Admin role, and that will require only one change, whereas, if you put the "Super User" or "Administrator" string directly in the view, you will have to change it wherever you've used the value.

提交回复
热议问题