tags:

views:

118

answers:

3

I’m using the Membership Provider and would like to display a list of all the users and their First Name, Last Name etc using the GetAllUsers function.

I'm having trouble understanding how to implement this function in MVC.

Has anyone implemented this in MVC or is there an easier way to list all the users in my application?

Any help or advise would be really helpful.

Controller

public ActionResult GetUsers()
{
    var users = Membership.GetAllUsers();
    return View(users);
}

View Model

public class GetUsers
    {
        [Required]
        [DisplayName("User name")]
        public string UserName { get; set; }

        [Required]
        [DisplayName("User name")]
        public string FirstName { get; set; }
    }

View

<%= Html.Encode(item.UserName) %>

Error

The model item passed into the dictionary is of type 'System.Web.Security.MembershipUserCollection', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Account.Models.GetUsers]'.

+3  A: 

What's the difficulty you have with it? the GetAllUsers method simply returns a collection of users that you can then display ... either manually, or using a grid component from a vendor like Telerik.

something like:

<% foreach(var user in Membership.GetAllUsers()) { %>
   <p>Name: <%= user.UserName %></p>
<% } %>

Obviously, heed the warning in the documentation:

Be careful when using the GetAllUsers method with very large user databases, as the resulting MembershipUserCollection in your ASP.NET page may degrade the performance of your application.

There is an overload which lets you do paging to get around this :-)

Joel Martinez
I'm trying to add the GetAllUsers function to the built in AccountModel.
Jemes
+1  A: 

@Jemes, the problem you're having is that you're passing a System.Web.Security.MembershipUserCollection as the model to your view and you specified that the model of your view was of type Account.Models.GetUsers. Change the type to System.Web.Security.MembershipUserCollection. However, if you're using the default Membership provider in your solution, you will not have the First Name available as the MembershipUser class doesn't have a FirstName property.

uvita
Ok I see. Is it ok to call MembershipUserCollection straight into my views or is it better to build a ViewModel with the MembershipUserCollection and calling that ViewModel into my View?
Jemes
In this case, where you are only displaying data of each membership user it's fine. If you're going to aggregate data from different entities or show data from the user profile, then it would be better to build a ViewModel containing a property for each of the fields you're displaying.
uvita
I need to display each users role with the data from the MembershipUserCollection so I guess a ViewModel is the way to go?
Jemes
Absolutely, create a UserViewModel with Username, Role or Roles (in case the user may have more than one) and any other properties you need. You can have a thin layer responsible for creating ViewModels from your business entities or use automapper which is an excellent tool for that kind of mappings.
uvita
+1  A: 

View

Inherits="System.Web.Mvc.ViewPage<MembershipUserCollection>"

<ul>
       <%foreach (MembershipUser user in Model){ %>

       <li><%=user.UserName %></li>

       <% }%>
</ul>

Controller

public ActionResult Admin()
        {
            var users = Membership.GetAllUsers();
            return View(users);
        }
Jemes
This helped me, thanks. What is the reason `var` cannot be used instead of `MembershipUser` in this case?
JustinStolle