views:

92

answers:

2

hi, i am having a dropdown box showing list of roles, i used Roles.GetAllroles() for showin' all roles in dropdown box but i don't wanna show a role named "Admin" in the dropdown box.

+4  A: 

(1) After you create the Selectlist in the controller removed the Admin ListItem

SelectList sl =  new SelectList(Roles.GetAllRoles(), "roleName")
//remove items as needed
ViewData["roleName"] = sl;

Or (2) step through the GetAllroles() collection and don't add the role if it is the Admin role.

Glennular
thanx, i am using mvc 1.0 so my code is <%= Html.DropDownList("roleName") %> and conrtoller code isViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");can u please tell how to do step 1 ?
FosterZ
I updated option one above to be MVC related
Glennular
+1  A: 

Try creating an extension method on the provider type and wrap the call. This will allow you to add additional criteria later if needed.

public static class RolesExtension
{
    public static string[] GetAllNonAdminRoles(this RoleProvider providerInstance)
    {
        return (from role in providerInstance.GetAllRoles()
                where !role.Equals("Admin", StringComparison.InvariantCultureIgnoreCase)
                select role).ToArray();
    }
}

Instead of ...

System.Web.Security.Roles.GetAllRoles();

Call this ...

System.Web.Security.Roles.Provider.GetAllNonAdminRoles();
JoeGeeky