Rob Conery has a rule of thumb that states "if there is IF, make an assistant . " Personally, I would say "use sparingly." I avoid this as much as possible because it complicates the unit test task.
In a situation where you want to hide elements based on user roles: for simple scenarios, I would probably put the check directly in the view. However, I try, however, to make them more concise and verified. Therefore, instead of:
@if (HttpContext.Current.User.IsInRole("admin") {
I would do something like:
@if (Model.UserIsAdmin) {
On the other hand, if these types of checks began to pop up across all your representations, I would probably first create elements conditionally in the viewmodel area, and then just show what was built. Hope this helps.
source share