Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hiding Html.ActionLinks based on Role-based security
- @section Contextual {
- <div>@Html.ActionLink("Create New", "Create")</div>
- <div>@Html.ActionLink("Generate Report", "Report")</div>
- <div>@Html.ActionLink("Other Stuff", "Other")</div>
- }
- [Authorize(Roles = "Editor")]
- public ActionResult Create()
- {
- // stuff
- }
- @section Contextual {
- @if (User.IsInRole("Editor"))
- {
- <div>@Html.ActionLink("Create New", "Create")</div>
- }
- <div>@Html.ActionLink("Generate Report", "Report")</div>
- <div>@Html.ActionLink("Other Stuff", "Other")</div>
- }
- // on the controller
- viewModel.CanCrete = User.IsInRole("Editor");
- // ...snip...
- return View(viewModel);
- }
- public static string If( this string s, bool condition )
- {
- return condition ? s : String.Empty;
- }
- @section Contextual {
- <div>@Html.ActionLink("Create New", "Create").If(Model.CanCrete)</div>
- <div>@Html.ActionLink("Generate Report", "Report")</div>
- <div>@Html.ActionLink("Other Stuff", "Other")</div>
- }
Add Comment
Please, Sign In to add comment