1 Replies - 199 Views - Last Post: 02 April 2019 - 06:26 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 219
  • Joined: 01-July 12

ASP.Net MVC C# Assign Roles off checkbox and list of users

Posted 02 April 2019 - 02:35 PM

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="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

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.Net MVC C# Assign Roles off checkbox and list of users

#2 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 219
  • Joined: 01-July 12

Re: ASP.Net MVC C# Assign Roles off checkbox and list of users

Posted 02 April 2019 - 06:26 PM

This is what i currently came up with. Its not 100% finished yet but I can get a user and assign the user to an admin it for sure works with 1 user i have to test more than one.
I'd be interested in any input or comments to what I am doing.
Thank you
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AssignAdmin(AssignRolesModel model)
        {
            try
            {
                if(model.lstUsers == null)
                {
                    TempData["Error"] = "There are no Users to Assign Roles";
                    model.lstUsers = UserList();
                    model.lstAdmins = AdminList();
                    return View(model);
                }

                var selectedUsersCount = (from user in model.lstUsers
                                          where user.SelectedUsers == true
                                          select user).Count();
                if(selectedUsersCount == 0)
                {
                    TempData["Error"] = "You have not Selected any User to Assign Roles";
                    model.lstAdmins = AdminList();
                    model.lstUsers = UserList();
                    return View(model);
                }

                if (ModelState.IsValid)
                {
                    List<UserModel> users = new List<UserModel>();
                    ApplicationUser au;
                    var store = new UserStore<ApplicationUser>(context);
                    var manager = new UserManager<ApplicationUser>(store);

                    model.CreatedBy = 1;
                    
                    foreach(var u in model.lstUsers)
                    {
                        if(u.SelectedUsers == true)
                        {
                            users.Add(u);
                        }
                    }

                    foreach(var u in users)
                    {
                        au = context.Users.Where(x => x.Id.Equals(u.UserId,
                            StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                        manager.AddToRole(au.Id, "Admin");
                    }

                    TempData["Success"] = "Roles Assigned Successfully";
                    return RedirectToAction("AssignAdmin");
                }
            }
            catch (Exception)
            {
                throw;
            }           

            return View();
        }

This post has been edited by Exceedinglife: 02 April 2019 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1