Home > DeveloperSection > Forums > How to get all users in mvc?
Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    October-09-2014 1:10 AM

 ASP.NET MVC ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 1054  View(s)
Rate this:
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]'.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-09-2014 1:14 AM

try this:

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);

        }


Don't want to miss updates? Please click the below button!

Follow MindStick