tags:

views:

1664

answers:

5

Hello. I have problem with ActionLink. I'd like to pass to my ActionLink parameter for my MessageController, for Edit action: to generate somthing like this /MessagesController/Edit/4

So I have ListView control with binding expression: <%# ((Message)Container.DataItem).CreationDate.ID %> and how to pass this ID to ActionLink as parameter to my Controller Edit action?

This doesn't work: <%= Html.ActionLink("my link", "Edit", "Message", <%# ((Message)Container.DataItem).ID %>, null) %>

A: 

Try this

<%= Html.ActionLink("my link", "Edit", "Message", new { id = ((Message)Container.DataItem).ID }) %>

You need to put it in the RouteData to get it to show up. Note I am assuming id is one of your route parts that is in your route definition.

Nick Berardi
A: 

Thanks, for Your answer but this doesn't solve my problem. Now Im getting this error:

The name 'Container' does not exist in the current context.

This works:

<%# ((Message)Container.DataItem).ID.ToString() %>

But this doesn't

<%= Html.ActionLink("My Edit Link", "Edit", "Message", new { id =((Message)Container.DataItem).ID }) %>

dario
A: 

My personal solution is:

<a href="<%= ResolveUrl("~/Messages/Edit/") %> <%# ((Message)Container.DataItem).ID %>">Edit</a>

But how to do this with ActionLink?

dario
+5  A: 

In MVC you are not supposed to databind from the view in the way that you have. The data that you want to pass to the ActionLink method needs to be added to ViewData in your controller. Then in the view you retrieve it from ViewData:

<%= Html.ActionLink("My Edit Link", "Edit", "Message", new { id = ViewData["id"] }) %>
liammclennan
Not sure if it's because it's not in the ViewData but when I do this with an int parameter I get a bad link. The code from below with the additional "null parameter works though. <%= Html.ActionLink("text", "action", "controller", new{...}, null) %>
David in Dakota
+3  A: 
<%= Html.ActionLink("My Edit Link", "Edit", "Message", new { id = ((Message)Container.DataItem).ID }, null) %>
This is the right answer but Container should be replaced with Model or some other ASP.NET MVC construct.
mare