1

I have a JavaScript file in which I need to generate an action link via Razor syntax. Here's my code so far.

@{
  int ID = @:cardId;
  @:row.insertCell(0).innerHTML = @Url.Action("Details", "Cards", new { Id = ID});
}

I want to nest my own text in the link. ie. <a>MyText</a> However, the Url.Action method is the only method I know of to create a link via Razor and I don't see an overload method that allows me to customize the text of the link.

tocoolforscool
  • 422
  • 1
  • 9
  • 24

1 Answers1

1

You should use an Html.ActionLink here. It has an extra argument that lets you make the link text.

@{
  int ID = @:cardId;
  @:row.insertCell(0).innerHTML = @Html.ActionLink("MyText", "Details", "Cards", new { Id = ID }, null)
}

Further, I doubt Url.Action would've helped you here anyways since it doesn't generate <a href="some_url_here"></a>, it only generates some_url_here. Html.ActionLink generates the entire thing. You can read up on the difference between the two here.

Community
  • 1
  • 1
Peter G
  • 2,773
  • 3
  • 25
  • 35