Click a <tr> to open a link

Asked

Viewed 726 times

1

On the site I am doing has the need that when it is clicked on a table row it redirects to another page, but the line id’s are dynamic, because each line 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");
    });
});
  • @Marconi did not understand the solution, I am still inexperienced in programming :P but thanks for the help

3 answers

3

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

$(document).ready(function () {
    $("tr").click(function () {
          window.location = 'suaPagina.aspx?id=' + $(this).attr('id');
    });
});
  • It worked, thank you very much!!

2

Puts a data-href in 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);
  });

 });

0

I don’t really like to mix javascript with Razor, but just try to include this function in your script, replacing the action and controller of the Url.Action method with the one you want to redirect.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.