royco
royco

Reputation: 5529

ASP.NET MVC: how to include <span> in link from Html.ActionLink?

I need the text of a link wrapped with <span> as in:

<a href="/foo.html"><span>Edit Group</span></a>

Can I do that with Html.ActionLink? This doesn't work:

<%=Html.ActionLink("<span>Edit Group</span>", "Edit", New With {.id = "bar"})%>

It just HTML encodes the < and > as &lt; and &gt;.

Is there a simple solution, or should I build the links by hand with Url.Action?

This is for use with jQuery-UI Tabs. Tab labels need to be wrapped in <span> to get animation when AJAX content is loading.

Upvotes: 18

Views: 27223

Answers (5)

user3405201
user3405201

Reputation: 31

What about this:

@{
   var link = Html.ActionLink("{0}", "Edit", New {id = "bar"}).ToString();
   var url = string.Format(link, "<span>Edit Group</span>");
}

@Html.Raw(url);

//NICE HACK: Recommend using previous advice and write a helper to wrap this up.

Upvotes: 3

RayLoveless
RayLoveless

Reputation: 21058

Here's a simple helperExtension example that worked for me:

http://forums.asp.net/p/1702210/4518688.aspx/1?Re+Quick+question+about+Ajax+ActionLink+and+span

Upvotes: 4

cory-fowler
cory-fowler

Reputation: 4088

You can also roll your own HtmlHelper Extension Method, I actually prefer this method as you can control the placement of ids, classes, and other attributes like a title.

Here is a blog post that I put together on the subject.

Upvotes: 8

David Andres
David Andres

Reputation: 31781

You can use the Url.Action helper method as a workaround (in case no other answers better fit your needs).

For example, the following can be in marked up in your view:

<a href="<%= Url.Action("Edit", 
                        New With {.id = "bar"}) %>">
 <span>Edit Group</span>
</a>

Upvotes: 23

womp
womp

Reputation: 116977

You'll need to do it with Url.Action, there's no way with Html.ActionLink as far as I know.

Upvotes: 11

Related Questions