Display Message Box after Delete data using jQuery AJAX in ASP.Net MVC

bigbear
 
on Apr 03, 2019 11:58 PM
2182 Views

Hello all,

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> 

 

    function clickMe() {
        var userid = $("#userAdmins").val();
        if (userid != "") {
            $.ajax({
                url: "RemoveAdmin/SuperAdmin",
                data: { userid: userid },
                type: "POST",
                //success: function () {
                //    alert("Success");
                //}
            });
        }
    }

 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");
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 04, 2019 02:05 AM

Hi bigbear,

You cannot show an messahe from a controller. There is one way communication from the client to the server. The client requests and the server gives a response.

You therefore need to use ajax success function when the response returns show a messagebox.

Ex:

Controller

[HttpPost]
public string RemoveAdmin(string userid)
{
    // Code for database stuff.
    TempData["Success"] = "Roles Removed Successfully";
    return TempData["Success"].ToString();
}

jQuery

<script type="text/javascript">
    function clickMe() {
        var userid = $("#userAdmins").val();
        if (userid != "") {
            $.ajax({
                url: "Home/RemoveAdmin",
                data: { userid: userid },
                type: "POST",
                success: function (response) {
                    alert(response);
                    window.location.href = "/Home/Index/";
                },
                error: function () {
                    alert("Error");
                }
            });
        }
    }
</script>