Guest User

Untitled

a guest
Aug 31st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. Hiding Html.ActionLinks based on Role-based security
  2. @section Contextual {
  3. <div>@Html.ActionLink("Create New", "Create")</div>
  4. <div>@Html.ActionLink("Generate Report", "Report")</div>
  5. <div>@Html.ActionLink("Other Stuff", "Other")</div>
  6. }
  7.  
  8. [Authorize(Roles = "Editor")]
  9. public ActionResult Create()
  10. {
  11. // stuff
  12. }
  13.  
  14. @section Contextual {
  15. @if (User.IsInRole("Editor"))
  16. {
  17. <div>@Html.ActionLink("Create New", "Create")</div>
  18. }
  19. <div>@Html.ActionLink("Generate Report", "Report")</div>
  20. <div>@Html.ActionLink("Other Stuff", "Other")</div>
  21. }
  22.  
  23. // on the controller
  24. viewModel.CanCrete = User.IsInRole("Editor");
  25. // ...snip...
  26. return View(viewModel);
  27. }
  28.  
  29. public static string If( this string s, bool condition )
  30. {
  31. return condition ? s : String.Empty;
  32. }
  33.  
  34. @section Contextual {
  35. <div>@Html.ActionLink("Create New", "Create").If(Model.CanCrete)</div>
  36. <div>@Html.ActionLink("Generate Report", "Report")</div>
  37. <div>@Html.ActionLink("Other Stuff", "Other")</div>
  38. }
Add Comment
Please, Sign In to add comment