Search code examples
asp.netasp.net-mvc-2html.actionlink

Turining An HTML <td> into a link ASP.NET MVC


I'd like to be able to turn rows of an html table into links to controllers. I figured something like

<td onclick="<%:Html.ActionLink("", "Index", new {id=item.user_id}) %>">

I'm using MVC 2

Thanks.


Solution

  • <td onclick="window.location='<%:Url.Action("Index", new {id=item.user_id}) %>'">
    

    The onclick attribute accepts some javascript code to execute. If you simply give it a URL, javascript doesn't know what to do with that.

    In the snippet above, you're setting the window.location property to the desired URL. This causes the browser to go there.

    EDIT: I also just realized that you were using the Html.ActionLink() method which actually generates an <a href=""></a> tag in your code. You'd be better off using the Url.Action() method, which actually generates a URL.