0 Replies - 550 Views - Last Post: 03 April 2019 - 06:27 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

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

Looking for a better way to remove Role with Button ASP.Net MVC

Posted 03 April 2019 - 06:27 PM

Hello all,
I currently have this working I have a submit button currently being used to assign roles. I have a second button to remove the roles. To remove the roles I call an $.ajax call and I pass my controller with the id to my action. This works BUT my TempData[msg] isn’t being stored so when the call finishes no message is displayed like when I add a role. Also the Roles are not being refreshed with my ‘return redirectToAction()’ at the end of my action method.
Here is what I have is there a way I can get those small things working or what are some more ideas to do this?
Thank you,
 //VIEW This is where I am getting my userId from this DropDownList @Html.AntiForgeryToken()

            <div class="row">
                <div class="col-md-4">
                    @Html.DropDownListFor(a => a.UserId,
                     new SelectList(Model.lstAdmins, "UserId", "Name"),
                     new { @class = "form-control", @id = "userAdmins" })
                    @* , @onchange = "SelectedValue(this)"*@
                    @Html.HiddenFor(a => a.UserId)
                    @Html.ValidationMessageFor(a => a.UserId)
                </div>
                <div class="col-md-4"></div>
                <div class="col-md-4"></div>
            </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" />
                        @* @Url.Action("RemoveAdmin", "SuperAdmin") *@

                        <button type="button" onclick="clickMe()"
                                class="btn btn-danger">
                            Remove Role
                        </button>

                        @Html.ActionLink("Cancel", "Dashboard", "SuperAdmin",
                         null, new { @class = "btn btn-danger" })
                    </div>                                                          <script type="text/javascript">
    //$(function () {

    function clickMe() {
        var userid = $("#userAdmins").val();
        if (userid != "") {
            $.ajax({
                url: "RemoveAdmin/SuperAdmin",
                data: { userid: userid },
                type: "POST",
                //success: function () {
                //    alert("Success");
                //},
                //error: function () {
                //    alert("Error");
                //}
            });
        }
        //window.location.href =
            //'Url.Action("RemoveAdmin", "SuperAdmin")/' + userid;
        //alert("val" + selectedValue);
    }
// CONTROLLER ACTION
[HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult RemoveAdmin(string userid)
        {
            ApplicationUser au = context.Users.Where(u => u.Id.Equals(userid,
                StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store);
            manager.RemoveFromRole(au.Id, "Admin");
            manager.AddToRole(au.Id, "User");

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



Is This A Good Question/Topic? 0
  • +

Page 1 of 1