I need to show all roles only for users that are in my database. I want to use this LINQ query:
var roles = from MembershipUser u in Membership.GetAllUsers()
select new
{
user = u,
roles = Roles.GetRolesForUser(u.UserName)
};
but now I need to display the roles in the view underneath each username with one role on a line, but not sure how to do that. I also need to have the ability to add and remove roles for users in the database. Any help would be greatly appreciated.
First give a name to the class created by the LinQ statement, so you can put the list of such classes as a property of the ViewModel.
than just do a nested foreach in your view. In the outermost foreach you render each user, while in the innermost foreach you list trhe roles of each user contained in the roles property of your class.
In order to delete/add a role you just need to use the AddUSerToRole and DeleteUserFromRole static methods of the Role class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace ContactWeb.Controllers
{
public class RoleController : Controller
{
public ActionResult GetAllUsers()
{
var roles = from MembershipUser u in Membership.GetAllUsers()
select new
{
user = u,
roles = Roles.GetRolesForUser(u.UserName)
};
return View();
}
public ActionResult AddRole(string rolename, string username)
{
Membership.AddRoleToUser(rolename, username);
}
public ActionResult RemoveRole(string rolename, string username)
{
Membership.DeleteUserFromRole(rolename, username);
}
}
}
and not sure if this is how the foreach loop should look like
<ul>
@foreach (User u in Membership.GetAllUsers())
@foreach (User u in ShowAllRoles())
{
<li>@String.Join(", ", u.Roles.ToArray())</li>
}
</ul>
I'm getting an error on the line: MyViewModel model = new MyViewModel { AllUsers = roles };
It says AllUsers is inaccessible due to its protection level.
and I'm not sure what code to put in your comments in Step 4
i'm getting an error now on the line with foreach (string role in u.UserRoles)
it says CS1061: 'System.Web.Security.MembershipUser' does not contain a definition for 'UserRoles' and no extension method 'UserRoles' accepting a first argument of type 'System.Web.Security.MembershipUser' could be found (are you missing a using directive
or an assembly reference?)
Thanks. I'm using a ContactListViewModel already with the view and using MyUser is giving an error: CS0246: The type or namespace name 'MyUser' could not be found (are you missing a using directive or an assembly reference?)
MyUser is the the name I invented for a class that you have to define! You can use another name obviously....The important that you define a class that contains both a MembershipUser and its associated roles. The code I wrote...is not supposed to run as
its is, but it is just an example for you to understand the way to proceed.
multiv123
Member
84 Points
47 Posts
showing user roles for users in database
Dec 04, 2011 04:30 PM|LINK
Hi,
I need to show all roles only for users that are in my database. I want to use this LINQ query:
var roles = from MembershipUser u in Membership.GetAllUsers()
select new
{
user = u,
roles = Roles.GetRolesForUser(u.UserName)
};
but now I need to display the roles in the view underneath each username with one role on a line, but not sure how to do that. I also need to have the ability to add and remove roles for users in the database. Any help would be greatly appreciated.
francesco ab...
All-Star
20888 Points
3277 Posts
Re: showing user roles for users in database
Dec 04, 2011 05:04 PM|LINK
First give a name to the class created by the LinQ statement, so you can put the list of such classes as a property of the ViewModel.
than just do a nested foreach in your view. In the outermost foreach you render each user, while in the innermost foreach you list trhe roles of each user contained in the roles property of your class.
In order to delete/add a role you just need to use the AddUSerToRole and DeleteUserFromRole static methods of the Role class.
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 05:35 PM|LINK
okay my RoleController looks like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace ContactWeb.Controllers
{
public class RoleController : Controller
{
public ActionResult GetAllUsers()
{
var roles = from MembershipUser u in Membership.GetAllUsers()
select new
{
user = u,
roles = Roles.GetRolesForUser(u.UserName)
};
return View();
}
public ActionResult AddRole(string rolename, string username)
{
Membership.AddRoleToUser(rolename, username);
}
public ActionResult RemoveRole(string rolename, string username)
{
Membership.DeleteUserFromRole(rolename, username);
}
}
}
and not sure if this is how the foreach loop should look like
<ul>
@foreach (User u in Membership.GetAllUsers())
@foreach (User u in ShowAllRoles())
{
<li>@String.Join(", ", u.Roles.ToArray())</li>
}
</ul>
asp.netmvc asp.netmvc3
francesco ab...
All-Star
20888 Points
3277 Posts
Re: showing user roles for users in database
Dec 04, 2011 06:53 PM|LINK
No
Step One) define a class to represent your user, say MyUser
This class have two properties:
public MembershipUser User {get; set;}
public IEnumerable<string> UserRoles {get;set;}
Step Two) define a ViewModel that will containg all your users, say MyViewModel.
This class will have a Property AllUsers of type IEnumerable<MyUser> to contain all user
Step 3) your action do:
var roles = from MembershipUser u in Membership.GetAllUsers()
select new MyUser
{
User = u,
UserRoles = Roles.GetRolesForUser(u.UserName)
};
MyViewModel model = new MyViewModel{AllUsers=roles};
return View(model);
Step 4) In your View you have:
@foreach (MembershipUser u in Model.AllUsers){
......some user property rendered here
@foreach (string role in u.UserRoles)
{
.....a role of user rendered here
}
}
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 07:21 PM|LINK
I'm getting an error on the line: MyViewModel model = new MyViewModel { AllUsers = roles };
It says AllUsers is inaccessible due to its protection level.
and I'm not sure what code to put in your comments in Step 4
asp.netmvc asp.netmvc3
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 07:27 PM|LINK
ok i forgot to include public...
i'm getting an error now on the line with foreach (string role in u.UserRoles)
it says CS1061: 'System.Web.Security.MembershipUser' does not contain a definition for 'UserRoles' and no extension method 'UserRoles' accepting a first argument of type 'System.Web.Security.MembershipUser' could be found (are you missing a using directive or an assembly reference?)
francesco ab...
All-Star
20888 Points
3277 Posts
Re: showing user roles for users in database
Dec 04, 2011 08:57 PM|LINK
sorry I made a mistake, u is MyUse, not MembershipUser
@foreach (MyUser u in Model.AllUsers){
......some user property rendered here
@foreach (string role in u.UserRoles)
{
.....a role of user rendered here
}
}
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 09:04 PM|LINK
Thanks. I'm using a ContactListViewModel already with the view and using MyUser is giving an error: CS0246: The type or namespace name 'MyUser' could not be found (are you missing a using directive or an assembly reference?)
francesco ab...
All-Star
20888 Points
3277 Posts
Re: showing user roles for users in database
Dec 04, 2011 09:08 PM|LINK
MyUser is the the name I invented for a class that you have to define! You can use another name obviously....The important that you define a class that contains both a MembershipUser and its associated roles. The code I wrote...is not supposed to run as its is, but it is just an example for you to understand the way to proceed.
Mvc Controls Toolkit | Data Moving Plug-in Videos
multiv123
Member
84 Points
47 Posts
Re: showing user roles for users in database
Dec 04, 2011 09:13 PM|LINK
I understand that. I defined MyUser in a MyUser.cs file in Models, but it doesn't seem to run.