Search code examples
c#asp.net-mvcasp.net-membership

ASP.NET MVC List All Users


I'm trying to show a list of all users but am unsure how to go about this using the MVC model.

I can obtain the list of all users via the Membership.GetAllUsers() method however if I try to pass this to the view from the ActionResult, I'm told that Model is not enumerable.


Solution

  • You have to set the View to accept an object of type MembershipUserCollection

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>" %>
    

    In your action:

     public ActionResult GetUsers()
            {
                var users = Membership.GetAllUsers();
                return View(users);
            }  
    

    then you can write in your view something like:

     <ul>
           <%foreach (MembershipUser user in Model){ %>
    
           <li><%=user.UserName %></li>
    
           <% }%>
    </ul>