Jamie Dixon
Jamie Dixon

Reputation: 54021

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.

Upvotes: 15

Views: 26927

Answers (5)

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

Upvotes: 0

BFree
BFree

Reputation: 103770

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.

Upvotes: 3

Anthony Conyers
Anthony Conyers

Reputation: 361

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 --%>
<% } %>

Upvotes: 0

user434917
user434917

Reputation:

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>

Upvotes: 29

nkirkes
nkirkes

Reputation: 2665

[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.

Upvotes: 0

Related Questions