Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top