Datatables editor with MVC - How to pass multiple Ids of selected rows as params for Delete Action?

Datatables editor with MVC - How to pass multiple Ids of selected rows as params for Delete Action?

DeependraSinghDeependraSingh Posts: 1Questions: 1Answers: 0
edited July 2017 in Free community support

Could someone help with this issue? Below is a part of the javascript code of the table editor:

var editor;
editor = new $.fn.dataTable.Editor({
ajax: {
      create: {
           type: 'POST',
           url: '@Url.Action("CreateUserData", "XYZ")',
      },
      edit: {
           type: 'PUT',
           contentType: 'application/json',
           url: '@Url.Action("EditUserData", "XYZ")',
           data: function (d) {
               var newdata;
               $.each(d.data, function (key, value) {
                    newdata = JSON.stringify(value);
               });
               console.log(newdata);
               return newdata;
               }
            },
      remove: {
           type: 'DELETE',
           dataType: "json",
           contentType: 'application/json; charset=utf-8',
           url: '@Url.Action("DeleteUserData", "XYZ")',
           //data: - mentioned as not necesssary in documentation
           success: function (data) {
              console.log('success');
           },
           error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
           }
        },
      reponsive: true,
      rowId: 'UserID',
      idSrc: "UserID",
      .
      .
      .

Here are the controller actions:

[HttpPost]
public JsonResult CreateUserData(SecurityDataModel user)
{
    return Json("Response from Create");
}

[HttpPut]
public ActionResult EditUserData(SecurityDataModel user)
{
    return Json("Response from Edit");
}

[HttpDelete]
public JsonResult DeleteUserData(string id)
{
    return Json("Response from Delete");
}

I am able to get values in my Edit/Create actions but the param for Delete action is always null. In the documentation from the website, it has been mentioned that if multiple rows are selected, rowids are passed as comma seperated values. In my case, the value is of type string. Any help will be appreciated.

Answers

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14
    edited July 2017

    Hi DeependraSingh, if your implementation is anything like mine the id's are sent through the URL in a string like "1,2,3,10,34". I do this to put them into an array of ints.

    int[] ids = id.Split(',').Select(int.Parse).ToArray();
    
    

    Then you can loop through and run your delete method :-)

    Shay

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    What does url: '@Url.Action("DeleteUserData", "XYZ")', resolve to? You can use _id_ in the URL to tell Editor where to put the ids (see ajax), which in turn leads to the method Shay suggests.

    Allan

This discussion has been closed.