I want to write an extension method that will allow me to disable a control based on a security setting. The code below works and accomplishes what I want. However - because it creates an object that represents all html attributes - I cannot specifiy additional attributes other than the disabled attribute that this code generates.
public static class SecurityHtmlHelper
{
public static object EnableForPermission(this HtmlHelper html, Permission permission)
{
if (Security.HasPermission(permission))
return new object();
else
return new { disabled = "disabled" };
}
}
Example of how the above is used:
@Html.ActionLink("permission test", "/", null, @Html.EnableForPermission(Permission.PM_PROCEDURE_ALT_SCEN_READ))
Desired usage example (does not build):
@Html.ActionLink("permission test", "/", null, new { @style ="xyz", @Html.EnableForPermission(Permission.PM_PROCEDURE_ALT_SCEN_READ)})
No I dont want to use javascript and yes I realize disabling a link does not prevent the user from navigating to a page there are other controls in place for that.
Thx.
For reference on disabled attribute: Correct value for disabled attribute