Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Bruce 122 posts 506 karma points
    Feb 26, 2019 @ 12:06
    Tom Bruce
    0

    Using ASP.Net Membership, get all users

    Hi everyone,

    I'm trying to use Membership.GetAllUsers() to create a list of all of my users in the database. However, I have a model that I use that maps out the user properties (First Name, Last Name, Email, etc.).

    How can I add all of the users into List

    Receiving an error Cannot implicitly convert type System.Collections.Generic.List

    at model.MemberList = tempMemberList;

    public ActionResult RenderMembers(ApplicationViewModel model)
    

    { var memberList = Membership.GetAllUsers();

    var tempMemberList = new List<MembershipUser>();
    foreach (MembershipUser member in memberList)
    {
        tempMemberList.Add(member);
    }
    
    model.MemberList = tempMemberList;
    
    return PartialView(GetViewPath1("_member-accounts"), model);
    

    }

    Model

    ublic List<MemberList> MemberList { get; set; }
    

    MemberList class

    public class MemberList
    { 
        public Guid Id { get; set; }
        public string Username { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Comment { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public DateTime LastLoginDate { get; set; }
        public DateTime LastLockoutDate { get; set; }
        public bool IsFrontendDev { get; set; }
        public bool IsBackendDev { get; set; }
    }
    

    Any pointers much appreciated.

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Feb 26, 2019 @ 13:01
    Nik
    100

    Hi Tom,

    Your issue is that you are trying to pass a List<MemberUser> into a List<MemberList> which is why you are getting a conversion issue.

    You'll have to do direct conversion between your MemberUser class and your MemberList class.

    for example (using Linq):

    model.MemberList = tempMemberList.Select(m => new MemberList{
        Username = m.UserName, //assuming MemberUser class has an UserName property
        Email = m.EmailAddress, //assuming MemberUser class has an EmailAddress property,
        ..... repeat for all properties you are mapping across ...
        }).ToList();
    

    Does that make sense?

    Nik

  • Tom Bruce 122 posts 506 karma points
    Feb 26, 2019 @ 13:59
    Tom Bruce
    0

    Yep thanks

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies