views:

1344

answers:

5

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.

Thanks for any advice on this one.

A: 

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

mannish
+2  A: 

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.

BFree
+6  A: 

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>
Marwan Aouida
A: 

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 --%>
<% } %>
Anthony Conyers
A: 

Can I show this user list in Telerik() Grid?