15

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.

2
  • Thanks everyone for the help. I'm new to this MVC stuff and didn't realise I could strongly type the Model. Cheers !! Commented May 17, 2009 at 16:09
  • 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(); } Commented Jul 23, 2013 at 22:53

5 Answers 5

29

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>
Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't seem to be working in MVC4 in my application
3

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

0

[Edit] Specifically, what does the view look like (e.g. what's it expecting to get as the model, how are you parsing the collection, etc.)

Can you post some code? I'm doing something similar, but am having no problems.

Comments

0

It sounds like you need to make your view strongly typed view. Have your view derive from ViewPage<MembershipUserCollection> instead of just ViewPage. Another solution is to cast your Model to MembershipUserCollection in your view:

<% var members = (MembershipUserCollection) ViewData.Model %>
<% foreach (MembershipUser user in members) { %>
       <%-- Do Stuff with the user --%>
<% } %>

Comments

0

Try asp.net application object

        string id = Session.SessionID.ToString();
        String[] users = new String[1000];
        users = (string[])Application["users"];
        int count=0;
        for (int d=0;1000 >d ;d++ )
        {
            if (users == null) { users = new String[1000]; }
            count = d;
            if (users[d] == null) { break; }
        }
        users[count] = id;
        Application["users"] = users;
        string[] usersTable = (string[])Application["users"];
        for (int d=0;1000 >d ;d++ )
        {
            if (usersTable[d] == null) { break; }
        Label1.Text += usersTable[d].ToString()+" | ";

add to application object code

        Application["users"] = users;

retrive from applicaton object

        string[] usersTable = (string[])Application["users"];

this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.