ASP.NET MVC List All Users

26,521

Solution 1

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>

Solution 2

In your view page, on top, you need to set the type of the view page. IE:

On the top of your View, in the first line of the markup, you'll see something like this:

Inherits="System.Web.Mvc.ViewPage"

Change that to be:

Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>"

or whatever the type you're trying to pass to the view. The "Model" object will now be of type MembershipUserCollection which you can safely iterate over.

Share:
26,521
Jamie Dixon
Author by

Jamie Dixon

Co-founder https://leadpal.io

Updated on September 26, 2020

Comments

  • Jamie Dixon
    Jamie Dixon over 3 years

    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.

    • Jamie Dixon
      Jamie Dixon almost 15 years
      Thanks everyone for the help. I'm new to this MVC stuff and didn't realise I could strongly type the Model. Cheers !!
    • Aaron Hoffman
      Aaron Hoffman almost 11 years
      With ASP.NET MVC 4 and the SimpleMembershipProvider Membership.GetAllUsers() is not supported. Instead use, using (var ctx = new UsersContext()) { var users = ctx.UserProfiles.ToList(); }
  • Dashrath
    Dashrath almost 7 years
    it doesn't seem to be working in MVC4 in my application