Search code examples
c#javascriptasp.net-mvcasp.net-mvc-3webgrid

Delete webgrid row using javascript?


I have a webgrid in mvc3. It has the column delete. Upon clicking it, I want to run a Javascript function to redirect the user to a controller's action by passing a row id as the parameter to the Javascript function.

How would I do this? The column is not an Htmlactionlink.


Solution

  • Assuming this is the way you have the rows:-

    <tr id="thisRowId">
      .
      .
      .
     <td>
        <a id="deleteBtn" data-rowId="thisRowId">delete</a>
     </td>
    <tr>
    

    Have a generic function for your delete click

    $('#deleteBtn').click(function(){
    
    var id =   $(this).data('rowId'); // or use $(this).closest('tr').attr('id');
    
    $.ajax({
    url: "controller/action",
    type: 'Delete', // assuming your action is marked with HttpDelete Attribute or do not need this option if action is marked with HttpGet attribute
    data: {'id' : "'" + id  "'"} // pass in id here
    success : yoursuccessfunction
    });
    
    };