0

I'm totaly new to ASP.NET and have big issue with understanding how Membership.GetAllUsers work.

At the moment im working with MVC 4 project. So, I have a task to make table of Users and their Role but I dont have a clue how to do this. I know that silly situation but I realy need help.

1 Answers1

2

Just in case someone comes with the same question. To manipulate the users without binding them to any component it is quite simple:

First the controller, we get all the users:

public class UserController : Controller
{
    //
    // GET: /Admin/User/

    public ActionResult Index()
    {
        return View(Membership.GetAllUsers());
    }

}

Be carefull in case you have too many users this can become a performance issue, I mean to retrieve all in one go without filtering or pagination.

Then in the view just interate through the collection, and for each user just the the roles and interate through them as well:

@model MembershipUserCollection

@{
    ViewBag.Title = "Index";
}

@foreach (MembershipUser user in Model)
{
    var roles = Roles.GetRolesForUser(user.UserName);
     <p>
         @user.UserName
         <br />
            @foreach (var role in roles)
            {
                <span>@role</span>, 
            }
      </p>
}
Alfonso Muñoz
  • 1,609
  • 17
  • 24