I use @RenderSection("Contextual", false) in my _Layout.cshtml to allow various views to display their specific content there. Some do not, others do.
In addition, I use role-based protection and ActionFilter to control whether a particular user has access to certain controller actions and thus the routes on my site.
What I would like to do is provide an @RenderSection("Contextual", false) section on my _Layout.cshtml, and then indicate that a particular page provides any contextual material for that page and have an appropriate controller handle to check if the user to perform some action and maybe even see that there are options, but I'm not sure I think about it correctly. Here's how to do it now:
Now I have a section in one of my Index.cshtml files, for example:
@section Contextual { <div>@Html.ActionLink("Create New", "Create")</div> <div>@Html.ActionLink("Generate Report", "Report")</div> <div>@Html.ActionLink("Other Stuff", "Other")</div> }
and then in my respective controller I have something like this:
[Authorize(Roles = "Editor")] public ActionResult Create() {
This will work the way I want (no editors will be able to create new elements), but the Create entry is available to everyone. I can do something like this:
@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> }
And it works pretty well, hiding the Create link from non-editors, but I am on a worry about whether it is good or not to do this, and I see that along the way I have a situation where the rules change, and then I have two places for synchronization: the controller action attribute and the code in the view.
Is this a smart approach? Is there a better way to approach this?