List of Users and Role using Membership Provider

Posted by Jemes on Stack Overflow See other posts from Stack Overflow or by Jemes
Published on 2010-05-13T15:21:41Z Indexed on 2010/05/14 1:44 UTC
Read the original article Hit count: 235

Filed under:

I’m trying to produce a view to show a list of users and their role using the built in membership provider.

My model and controller are picking up the users and roles but I’m having trouble displaying them in my view.

Model

public class AdminViewModel
    {
        public MembershipUserCollection Users { get; set; }
        public string[] Roles { get; set; }
    }

Controller

public ActionResult Admin()
{
      AdminViewModel viewModel = new AdminViewModel
      {
            Users = MembershipService.GetAllUsers(),
        Roles = RoleService.GetRoles()
      };

      return View(viewModel);
}

View

Inherits="System.Web.Mvc.ViewPage<IEnumerable<Account.Models.AdminViewModel>>"

<table>

<tr>
    <td>UserName</td>
    <td>Email</td>
    <td>IsOnline</td>
    <td>CreationDate</td>
    <td>LastLoginDate</td>
    <td>LastActivityDate</td>
</tr>

<% foreach (var item in Model) { %>

<tr>
    <td><%=item.UserName %></td>
    <td><%=item.Email %></td>
    <td><%=item.IsOnline %></td>
    <td><%=item.CreationDate %></td>
    <td><%=item.LastLoginDate %></td>
    <td><%=item.LastActivityDate %></td>
    <td><%=item.ROLE %></td>
</tr>

<% }%>

</table>

© Stack Overflow or respective owner

Related posts about asp.net-mvc