In roles I am creating a list of Role, RoleList, and using this array
of string as follows:
RoleList.Where(r => (userRoles == null ||
userRoles.Contains(r.Type.ToString()))
So if userRoles is empty, nothing was passed, all records are used.
If not then from RoleList get only the items which r.Type is contained
in RoleList.
But now I am not sure if I can use:
userRoles == null
Because I think I can't pass the string array as null.
A concise-but-complete code sample would be helpful. Passing "null" or
"new string[0]" should work fine, I think, at least given what you've
showed us so far. So if it doesn't work, there's probably something else
going on.
But you need to show everything for us to figure out what that "something
else" is.
Pete
Sorry, for the delay.
Basically, I have a class Role with Type, Description, Open, ...
properties that holds information on each of my application role.
Inside this class I have a method named List that returns the list of
roles to populate and input, checkboxes, etc ... So the format might
differ.
And sometimes I might need to filter the list ...
On my MVC view I want to call the method list but not directly ... I
prefer to call the a method on the View's ViewData object. So I have:
public static IList<Role> Roles(CultureInfo culture, RoleType?
type, bool? open, bool? beginWithEmpty, string[] userRoles) {
return Role.List(culture, type, open, beginWithEmpty,
userRoles);
}
public static IList<Role> List(CultureInfo culture, RoleType?
type, bool? open, bool? beginWithEmpty, string[] userRoles) {
IList<Role> roles = new List<Role>();
if (beginWithEmpty ?? false) roles.Add(new Role { Type = null,
Description = "---", Open = true });
switch (culture.TwoLetterISOLanguageName.ToLower()) {
case "pt":
roles.Add(new Role { Type = RoleType.Administrator,
Description = "Administrador", Open = false });
roles.Add(new Role { Type = RoleType.Collaborator,
Description = "Colaborador", Open = false });
// ...
break;
}
return roles.Where(r => (userRoles == null ||
userRoles.Contains(r.Type.ToString())) && (open == null || r.Open ==
open) && (type == null || r.Type == type)).OrderBy(r =>
r.Description).ToList();
} // List
And in my view I have:
<%foreach (Role role in
ViewData.Get<AccountBook>("Account").Roles(Thread.CurrentThread.CurrentCulture,
RoleType.Administrator, false, true, null)) {%>
And I am getting the error:
Member
'MyApp.Models.AccountBook.Roles(System.Globalization.CultureInfo,
MyApp.Security.Membership.RoleType?, bool?, bool?, string[])' cannot
be accessed with an instance reference; qualify it with a type name
instead
What am I doing wrong?
Thank You,
Miguel