Click on a tr to open a link

1

In the site I'm doing, there's a need to click on a row of the table it redirects to another page, but the row's id's are dynamic because each row should redirect to a page that shows more information.

My View:

@foreach (projeto1.Entidade.PessoaEntidade item in Model)
        {
            <tr id="[email protected]">
                <td id="[email protected]" style="width:10%">
                    @item.id
                </td>
                <td style="width:40%">
                    @item.Nome
                </td>
                <td style="width:40%">
                    @item.Sobrenome
                </td>
                <td style="width:10%">
                    <input type="button" value="Alterar" id="partionalViewUpdate" onclick="Teste(@item.id)" />
                </td>
            </tr>

My Script:

$(document).ready(function () {
    $("tr").click(function () {
           alert("Clicado");
    });
});
    
asked by anonymous 21.03.2017 / 15:05

3 answers

3

Use window.location and concatenate with the ID of your tr :

$(document).ready(function () {
    $("tr").click(function () {
          window.location = 'suaPagina.aspx?id=' + $(this).attr('id');
    });
});
    
21.03.2017 / 15:10
2

Put a date-href in the tr and a script

HTML

<tr data-href="http://www.example.com.br/link"></tr>

Script


 $(document).ready(function () {

  $("tr").click(function() {
    var url = $(this).attr("data-href");
    window.open(url);
  });

 });

    
21.03.2017 / 15:23
0

I do not really like to mix javascript with Razor, but just try to include this function in your script, overriding the action and controller of the Url.Action method for what you're trying to redirect.

function Teste(id)
{
      window.location = '@Url.Action("Action","Controller")?id=' + id;
}
    
21.03.2017 / 15:21