Search code examples
ajaxasp.net-mvcactionlink

Encode dataview value in ASP.NET MVC Actionlink


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?


Solution

  • 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.