Make jQuery datatable row an asp-action link when clicked

I am trying to create a jQuery datable that when user clicked on a table row the asset Id is passed to the ViewAsset controller. Thanks for your help

HTML markup:

<table id="tblAsset">
 <thead >
    <tr>
        <th>
           Asset Name
        </th>
        <th>
           Asset Type
        </th>
        <th>
           Data Type
         </th>
   </tr>
   </thead>
   <tbody>

  @foreach (var item in Model)
   {
     // Onclick function for each row

     <tr onclick="ViewAsset(@item.Id)">

        <td>
         @Html.DisplayFor(modelItem => item.AssetName)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.AssetType)
         </td>

         <td>
         @Html.DisplayFor(modelItem => item.TypeofData)
         </td>
    </tr>
   }
  </tbody>
 </table>

jQuery datatable

    <script type="text/javascript">
    $(document).ready(function () {
        $('#tblAsset').DataTable({
            "pageLength":10,
            "order": [[1, "asc"]],
        }
        );

    });

    // jQuery Function to call the controller

    function ViewAsset(id) {
        window.location.href = "Assets/ViewAsset/" + id;
    }
</script>

Controller action link

 <a asp-action="ViewAsset" asp-route-id="@item.Id">

Answer

I found a better way to solve the issue. Post for people with similar issue in the future

<tr style="cursor: pointer;" onclick="location.href ='@(Url.Action("ViewAsset", "Assets", new { id = item.Id}))'">  

Leave a Reply

Your email address will not be published. Required fields are marked *