views:

39

answers:

2

Our designers have come up with button styles for an application which require the addition of <span> tags inside the <a> tags of our links.

In ASP.NET we implemented this by adding an App_Browsers entry for Link Buttons.

How would I go about doing this in ASP.NET MVC?

I've contemplated creating my own versions of all of the various HTML helper functions for creating ActionLinks and RouteLinks but this seems to be quite a 'brute force' way of doing things.

Is there a nice elegant way of doing it?

I know we could write some simple jQuery to do it, but we'd rather have the markup coming out of the server correctly in the first place.

+1  A: 

Actually I think writing a new helper is exactly the way I would go. Seems to me that that's exactly what they are there for and it makes them very re-usable too.

griegs
Hi Griegs. My concern with this is that there are so many different versions of the methods that I'll have lots of variations to write. As new verisons get added, I'll have to keep pace. Any idea if there's a good place to 'post-process' the HTML before it gets sent? Thanks.
Chris Roberts
Sounds like a typical problem once designers get involved yeah. As for a good place to do this? Not that springs to mind. I'd take a look at all variations and see if there is a helper I could write that would give me a base. Then write another helper which might flesh out the base to give me others. These sorts of things are always painful.
griegs
You could build a base span, the thing in your anchor, build on it and then write a helper that takes the span as an argument maybe.
griegs
+1  A: 

You could always write one extension method, that takes another one (one of the built-in ones) as an argument, and wrappes the <span> around your link text before calling it. It should be quite easy to do with lambdas...

public static string SpanLink(this HtmlHelper helper,
    string linkText, object args, Action<string> action) 
    where TController : IController
{
    action("<span>" + linkText + "</span>", args);
}

And to call it:

<%= Html.SpanLink<HomeController>("link text", (s) => Html.ActionLink<HomeController>(c => c.Index(s));

(This code is typed directly into the answer field of SO - I haven't even checked it to make sure it compiles. So bear with me if it doesn't work on the first try...)

Tomas Lycken
Looks interesting! I'll give it a try, thanks.
Chris Roberts