Pregunta

I'm trying to bind data to Action Link within Knockoutjs foreach loop. This code works fine

<ul data-bind="foreach: ItemList">
    <li>
        <a data-bind="attr: { 'href': '@Url.Action("Items", "ItemController")' }" >
              LinkText
         </a>
    </li>
</ul>

But I also need to bind a parameter and bind the LinkText with knockoutjs. I tried different code samples but nothing seems to work.

Final code should be something like,

<ul data-bind="foreach: ItemList">
    <li>
        <a data-bind="attr: { 'href': '@Url.Action("Items", "ItemController")', new { id = DataBindId)' }" >
              DataBindName
         </a>
    </li>
</ul>

How can I make this work?

¿Fue útil?

Solución

Try this...

<a data-bind="attr: { 'href': '@Url.Action("Items", "ItemController")?id=' + DataBindId }, text: DataBindName" >
 </a>

Which should output something like...

<a data-bind="attr: { 'href': '/Item/Items?id=' + DataBindId }, text: DataBindName" >
 </a>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top