Assign Roles to Users in ASP.Net MVC

Last Reply on Apr 03, 2019 03:49 AM By dharmendr

Posted on Apr 02, 2019 11:56 PM

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.

This question does not have replies that have been liked.