views:

117

answers:

1

I have a number of permissions, and based on a set of conditions these permission determine if a user can see certain features. I have written a helper function for this as the logic in the view became quite extensive.

Essentially I'm looking for a function the same as Html.ActionLink that I can access from a class file (Ideally if I can access the Helper that would be great) So I can do somthing like so,

public static string GetAdminLinks()
{
    if(PermCheck)
    {
        return(Html.ActionLink(...));
    }
}

Any sugestions?

A: 

It largely depends on how your permission check is implemented (and of which information it needs to determine the user's permissions). Anyhow, I'd implement it as an extension to the HtmlHelper class.

Somewhere in your App_Code:

public static class HtmlHelperExtensions {
    public static string SecureActionLink(this HtmlHelper htmlHelper, string action, string controller){
        if(PermCheck)
            return htmlHelper.ActionLink(action, controller);
        else
            return string.Empty;
    }

    //add other ActionLink overrides if you like...
}

Then you'll be able to call the extension method from anywhere in your ViewPages without any code behind.

Lck