views:

1045

answers:

3

I am using the Microsoft Ajax Template DataView to bind values to a template. I can do this and it works as you'd expect:

<h3>{{ID}}</h3>
<p>{{Address}}</p>

However I am trying to build an action link that has the ID in it.

<h2><%= Html.ActionLink(Html.AttributeEncode("{{Name}}"), "Index", "Restaurant", new { Id = Html.AttributeEncode("{{ID}}") }, null)%></h2>

The name is shown as the link text as I wanted but the link doesn't include the ID, instead it has %7B%7BID%7D%7D

How would I get the Id to be properly parsed and added to the link?

A: 

It's possible that the extra brackets are throwing it off. Try assigning the values to variables and using the variables in the ActionLink. You could do the assignment at the top of the view and then reuse them throughout the view as well. to keep from having to re-encode them everywhere.

  <% var id = Html.AttributeEncode( "{{ID}}" );
     var name = Html.AttributeEncode( "{{Name}}" );
   %>
  <h2><%= Html.ActionLink(name, "Index", "Restaurant", new { Id = id }, null)%></h2>
tvanfosson
A: 

Finally got it to work, I don't know if I was being stupid or if it's the lack of documentation but here is how to bind the dataview value to a link.

 <h2><a sys:href="{{'Restaurant/Index/' + ID}}">{{Name}}</a></h2>

The actual url route part needs to be in single quotes and you need to use sys:href instead of href.

dean nolan
A: 

Its good practice to include '<%= Url.Action("Index", "Restaurant")%>' when builing a manual url to avoid problems with the application name and conflicting urls.

Ayo
When I add the Url.Action the url ends up as /Restaurant3Where 3 is the correct ID. Maybe I'm doing that wrong.
dean nolan
'<%= Url.Action("Index", "Restaurant")%>' + '/' + ID
Ayo
it only builds the url structure it doesn't know if you will be adding anything to it.
Ayo