locked
Using ASP.Net Membership, get all users RRS feed

  • Question

  • User-1664485818 posted

    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<model.MemberList>

    Receiving an error Cannot implicitly convert type System.Collections.Generic.List<system.Web.Membership? to System.Collections.Generic<MemberList>

    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

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

    Tuesday, February 26, 2019 11:26 AM

Answers

  • User1120430333 posted

    model.MemberList = tempMemberList;

    That above is doing a cast. However, each list was derived in two different namespaces System.Collections.Generic.List<system.Web.Membership to System.Collections.Generic<MemberList>. The highlighted are the namespaces. and .NET will not allow a cast across two different namespace.

    Secondly, system.Web.Membership can never be a memberList, because they are two different objects. Your naming convention is wrong for memberlist the class is wrong. It should be Member, because it's just a single class. The 'List" is a plural definition for a collection. A single class that you have defined as MemberList is not a collection/list object.

    model.MemberList = tempMemberList;

    In the above, you could do an AddRange() that allow you to address object in one list make a new object for the other list map one object to the other and add the new object to the other list.

    You see the example where a collection of dtos is coming back, and the dtoproject in dtos is being mapped to a VM.Project and loaded in VM.Projects using the Addrange();

     public ProjectViewModels GetProjectsByUserId(string userid)
            {
                var vm = new ProjectViewModels {Projects = new List<ProjectViewModels.Project>()};
    
                var dtos = _webApi.GetProjsByUserIdApi(userid).ToList();
                
                vm.Projects.AddRange(dtos.Select(dto => new ProjectViewModels.Project()
                {
                    ProjectId = dto.ProjectId,
                    ClientName = dto.ClientName,
                    ProjectName = dto.ProjectName,
                    Technology = dto.Technology,
                    ProjectType = dto.ProjectType,
                    StartDate = dto.StartDate,
                    EndDate = dto.EndDate,
                    Cost = dto.Cost
                }).ToList());
    
                return vm;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 26, 2019 12:08 PM