Tutorial :ASP.NET MVC List All Users



Question:

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


Solution:3

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


Solution:4

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


Solution: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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »