Delete Record From Data Base Without Reload page using Ajex And jquery in Asp.net MVC

Controller Code   :



[HttpPost]
        public JsonResult Delete(int id)
        {
            bool result = false;
                try
                {
                    obj.Delete(id);
                    result = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    // Provide for exceptions.
                }   
            return Json(result, JsonRequestBehavior.AllowGet);
        }



View :


@model IEnumerable<Asp.net_MVC_Using_Services.localhost.AdminLogin>

@{
    ViewBag.Title = "NewList";
}

<h2>NewList</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Password)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isActive)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CreationDate)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
    <tr id="row_@item.id">
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.isActive)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreationDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
            @Html.ActionLink("Details", "Details", new { id = item.id }) |
            <a href="#" data-toggle="popover" class="DeleteBtn" data-trigger="click" id="@item.id">Delete</a>
        </td>
    </tr>
    }

</table>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
     $(".DeleteBtn").click(function () {
            var id = $(this).attr('id');


            $.ajax({
                url: '@Url.Action("Delete", "Home")/' + id,
                method: "POST",
                data: { id: id },
                success: function (result) {
                    debugger
                    $("#row_"+id).remove();
                }
            })

        });
</script>




Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls