views:

29

answers:

1

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.

+1  A: 
<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.

jessegavin
Thanks. I couldn't get that to work because it complains that the name is null or undefined, but I tried thisonclick="window.location.href='/Controller/Method/<%:item.user_id%>'" and it liked it
Peter
I just edited my answer while you were posting that comment. If you use the new edited snippet, it will work. I would avoid "hard coding" your links like that. Using the Url.Action() method will do you better down the road.
jessegavin
That's perfect. I knew there had to be a better solution.
Peter
Well if you like the answer, you should click the little checkbox just to the left of it to mark it as ACCEPTED. *cough cough*
jessegavin