18

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.

5 Answers 5

23

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yes, that's what I ended up doing.
11

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

Comments

8

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.

2 Comments

This is awesome. But honestly Razor team need to realize that there is a fair demand for this sort of customization in link, and they should consider providing an option for Raw( _ ) rendering in action link.
Have you looked into TagHelpers? channel9.msdn.com/Shows/Web+Camps+TV/…
4

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

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.