Assign Roles to Users in ASP.Net MVC

bigbear
 
on Apr 02, 2019 11:56 PM
Sample_209601.zip
905 Views

Hello everyone.

I have a list of my Admins in a dropbox and a list of my Users in a panel with their name and a checkbox for their name.

I have a button in my panel so I can assign the users to an admin.

I am wondering how I would set this up in my controller assigning a role to a user isn't hard but doing It this way im not 100% sure about. Here is what I have.

my view

        <div class="panel-body">
            <div class="clearfix"></div>
            <div class="row">
                @for(var i = 0; i < Model.lstUsers.Count(); i++)
                {
                    <div class="col-md-4">
                        <div>
                            @Html.CheckBoxFor(u => Model.lstUsers[i].SelectedUsers)
                            <label>
                                @Html.DisplayFor(u => Model.lstUsers[i].Name)
                                @Html.HiddenFor(u => Model.lstUsers[i].UserId)
                                @Html.HiddenFor(u => Model.lstUsers[i].Name)
                            </label>
                        </div>
                    </div>
                }
            </div>
            <br />
            <div class="row">
                <div class="form-group">
                    <div class="col-md-offset-0 col-md-12">
                        <input type="submit" value="Assign Role" class="btn btn-success" />
                        @Html.ActionLink("Cancel", "Dashboard", "SuperAdmin",
                         null, new { @class = "btn btn-danger" })
                    </div>
                </div>
            </div>
        </div>

Model

    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 have a [httppost] and bring my assignrolesmodel object into the controller

Get the list of users which I have but I need to check the users checkboxes and assign them to admin.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 03, 2019 03:49 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? 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);
    }
    [HttpPost]
    public ActionResult Assign(AssignRolesModel role)
    {
        string adminName = AdminList().Where(x => x.UserId == role.AdminId).Select(x => x.Name).FirstOrDefault();
        foreach (var user in role.lstUsers)
        {
            string userId = user.UserId;
            string name = user.Name;
            bool selected = user.SelectedUsers;
            string assignToAdmin = selected ? adminName : "";
            // Code for Update record in database.
        }
        role.lstAdmins = AdminList();
        return View("Index", role);
    }

    // Get Admin List From Database.
    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" });
        lstAdmins.Add(new AdminModel { UserId = "3", Name = "Admin 3" });
        return lstAdmins;
    }

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

View

<body>
    <%using (Html.BeginForm("Assign", "Home", FormMethod.Post))
      {%>
    <div class="panel-body">
        <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>
        <br />
        <div class="row">
            <%for (var i = 0; i < Model.lstUsers.Count(); i++) {%>
            <div class="col-md-1">
                <%:Html.CheckBoxFor(u => Model.lstUsers[i].SelectedUsers) %>
                <label>
                    <%:Html.DisplayFor(u => Model.lstUsers[i].Name)%>
                    <%:Html.HiddenFor(u => Model.lstUsers[i].UserId)%>
                    <%:Html.HiddenFor(u => Model.lstUsers[i].Name)%>
                </label>
            </div>
            <%} %>
        </div>
        <br />
        <div class="row">
            <div class="form-group">
                <div class="col-md-offset-0 col-md-12">
                    <input type="submit" value="Assign Role" class="btn btn-success" />
                    <%:Html.ActionLink("Cancel", "Dashboard", "SuperAdmin", null, new { @class = "btn btn-danger" }) %>
                </div>
            </div>
        </div>
    </div>
    <%} %>
</body>

Screenshot