Populate multiple DropDownList using single Model in ASP.Net MVC

Last Reply on Apr 02, 2019 02:22 AM By dharmendr

Posted on Apr 01, 2019 10:48 PM

I have a ASP.Net MVC5 C# site and I have a controller that I am trying to get a list of my users with role admin and a list of my users with role user.

I have a model that has a list of each. I would like to get a list of each of them so I can use it in my view.

Here is what I have and some of what I tried.

        //models I’m using                                                                      
        public class AdminModel
        {
            public string UserId { get; set; }
            public string Name { get; set; }
        }
        public class UserModel
        {
            public string UserId { get; set; }
            public string Name { get; set; }
            public bool SelectedUsers { get; set; }
            public string AssignToAdmin { get; set; }
        }
        public class AssignRolesModel
        {
            public List<AdminModel> lstAdmins { get; set; }
            [Required(ErrorMessage = "Choose Admin")]
            public string UserId { get; set; }
            public List<UserModel> lstUsers { get; set; }
            public int? AssignToAdmin { get; set; }
            public int? CreatedBy { get; set; }
        }
                                                                                    
        // Controller                                                                              
        public class SuperAdminController : Controller
        {
            private ApplicationDbContext context;

            public SuperAdminController()
            {
                context = new ApplicationDbContext();
            }
                                                                                
            // ASSIGN ADMINISTRATOR
            public ActionResult AssignAdmin()
            {
                var lstAdmins = from users in context.Users
                            from roles in users.Roles
                            join r in context.Roles 
                            on roles.RoleId equals r.Id
                            where r.Name.Equals("Admin")
                            select new AssignRolesModel()
                            {
                                lstAdmins = (users.Id, users.Name),
                            }
                return View();
            }
        }

my view

            @model MVC_TimeSh.Models.AssignRolesModel
            <div class="row">
                <div class="col-md-4">
                    @Html.DropDownListFor(a => a.UserId,
                     new SelectList(Model.lstAdmins, "UserId", "Name"),
                     new { @class = "form-control" })
                    @Html.ValidationMessageFor(a => a.UserId)
                </div>
                <div class="col-md-4"></div>
                <div class="col-md-4"></div>
            </div>

Not sure how to make a list of both of just one in my model.

Posted on Apr 02, 2019 02:22 AM

Hi bigbear,

Check this example. Now please take its reference and correct your code.

Model

public class AssignRolesModel
{
    public List<AdminModel> lstAdmins { get; set; }
    public List<UserModel> lstUsers { get; set; }
    public string AdminId { get; set; }
    public string UserId { get; set; }
    public int? AssignToAdmin { get; set; }
    public int? CreatedBy { get; set; }
}
public class AdminModel
{
    public string UserId { get; set; }
    public string Name { get; set; }
}
public class UserModel
{
    public string UserId { get; set; }
    public string Name { get; set; }
    public bool SelectedUsers { get; set; }
    public string AssignToAdmin { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        AssignRolesModel model = new AssignRolesModel();
        model.lstAdmins = AdminList();
        model.lstUsers = UserList();
        return View(model);
    }

    public List<AdminModel> AdminList()
    {
        List<AdminModel> lstAdmins = new List<AdminModel>();
        lstAdmins.Add(new AdminModel { UserId = "1", Name = "Admin 1" });
        lstAdmins.Add(new AdminModel { UserId = "2", Name = "Admin 2" });
        return lstAdmins;
    }

    public List<UserModel> UserList()
    {
        List<UserModel> lstUsers = new List<UserModel>();
        lstUsers.Add(new UserModel { UserId = "1", Name = "User 1", SelectedUsers = true, AssignToAdmin = "Admin 2" });
        lstUsers.Add(new UserModel { UserId = "2", Name = "User 2", SelectedUsers = false, AssignToAdmin = "Admin 1" });
        lstUsers.Add(new UserModel { UserId = "3", Name = "User 3", SelectedUsers = false, AssignToAdmin = "Admin 2" });
        lstUsers.Add(new UserModel { UserId = "4", Name = "User 4", SelectedUsers = true, AssignToAdmin = "Admin 1" });
        return lstUsers;
    }
}

View

<div class="row">
    <div class="col-md-4">
        Admin:<%:Html.DropDownListFor(a => a.AdminId, new SelectList(Model.lstAdmins, "UserId", "Name"),new { @class = "form-control" })%>
    </div>
    <div class="col-md-4">
        User:<%:Html.DropDownListFor(a => a.UserId, new SelectList(Model.lstUsers, "UserId", "Name"),new { @class = "form-control" })%>
    </div>
    <div class="col-md-4">
    </div>
</div>

Screenshot