简体   繁体   中英

How to simulate a click on a @Html.ActionLink in javascript?

I have this part of view :

@foreach (var item in Model.listeStations){
   <tbody>
       <tr id="ligneRecherche">
           <td id="tdId">
               @Html.DisplayFor(modelItem => item.nom)
           </td>
           <td id="tdData">
               @Html.DisplayFor(modelItem =>item.adr)
           </td>
           <td id="tdLangue">
               @Html.DisplayFor(modelItem => item.cp)
           </td>
           <td id="tdType">
               @Html.DisplayFor(modelItem => item.ville)
           </td>
           <td id="selection">
               @Html.ActionLink("Choisir", "DetailsStation", new { id=item.id })
           </td>
       </tr> 
   </tbody>
}
</table>

If one user click everywhere on one row of the table it performs the actionLink. It's possible to do that in JS or other ? Thanks in advance !

add class to your tr tag to select it with jquery.

<tr id="ligneRecherche" class="row">

and select clicked row with using jquery

$(".row").click(function(){
    var url = $('td a', this).attr('href');
    window.location = url;
});

And also your view should look like this: (remove tbody tag to out of foreach )

<tbody>
@foreach (var item in Model.listeStations){
   <tr id="ligneRecherche" class="row">
       <td id="tdId">
           @Html.DisplayFor(modelItem => item.nom)
       </td>
       <td id="tdData">
           @Html.DisplayFor(modelItem =>item.adr)
       </td>
       <td id="tdLangue">
           @Html.DisplayFor(modelItem => item.cp)
       </td>
       <td id="tdType">
           @Html.DisplayFor(modelItem => item.ville)
       </td>
       <td id="selection">
           @Html.ActionLink("Choisir", "DetailsStation", new { id=item.id })
       </td>
   </tr> 
}
</tbody>

one more info: id of tr and td tags are meaningless. because they are repeatitive elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM