ASP.NET MVC List All Users
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.
Comments
-
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 theActionResult
, I'm told that Model is not enumerable.-
Jamie Dixon almost 15 yearsThanks everyone for the help. I'm new to this MVC stuff and didn't realise I could strongly type the Model. Cheers !!
-
Aaron Hoffman almost 11 yearsWith 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 almost 7 yearsit doesn't seem to be working in MVC4 in my application