Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an ActionLink with HTML elements in the link text

In an ASP.NET MVC view I'd like to include a link of the form:

<a href="blah">Link text <span>with further descriptive text</span></a> 

Trying to include the <span> element in the linkText field of a call to Html.ActionLink() ends up with it being encoded (as would be expected).

Are there any recommended ways of achieving this?

like image 485
Giraffe Avatar asked Dec 31 '08 09:12

Giraffe


People also ask

What is an HTML ActionLink?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

How do I create a link in Cshtml?

Use @Html. ActionLink("Menus", "Menu", "yourController", null, new { @class = "page-scroll" }) and add a controller method named Menu that return the view.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

You could use Url.Action to build the link for you:

<a href="<% =Url.Action("Action", "Controller")%>">link text <span>with further blablah</span></a> 

or use Html.BuildUrlFromExpression:

<a href="<% =Html.BuildUrlFromExpression<Controller>(c => c.Action()) %>">text <span>text</span></a> 
like image 110
Casper Avatar answered Sep 20 '22 23:09

Casper