Sharp
Sharp

Reputation: 121

calling action method with javascript asp mvc

i am using Datatable jquery and i would like to call delete action on every row inside a button ...how can i redirect to action method delete of the controllers User with the right parameter?

var datatableVariable = $('#myTable').DataTable({

    ....
    columns: [
        ...column definition...{
            data: null,
            render: function(data, type, row) {

                return "<a href='#' class='btn btn-danger' onclick=DeleteData('" + row.id + "'); >Delete</a>";
            }
        },
    ],
});




function DeleteData(id) {
    if (confirm("Are you sure you want to delete ...?")) {
        Delete(id);
    } else {
        return false;
    }
}

function Delete(id) {
    var url = '@Url.Content("~/")' + "Users/Delete";

    $.post(url, {
        ID: id
    }, function(data) {
        if (data) {
            oTable = $('#myTable').DataTable();
            oTable.draw();
        } else {
            alert("Something Went Wrong!");
        }
    });
}

Upvotes: 0

Views: 42

Answers (1)

Dumi
Dumi

Reputation: 1434

I assume you just want to call your action method in the controller.

Use Ajax

$.ajax({
  type: "POST",
  url: "/Users/Delete",
  data: { ID: id},
  success: {
  },
});

Upvotes: 1

Related Questions