开发者

How to alter the rendering of links in ASP.NET MVC?

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 creatin开发者_StackOverflowg 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.


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.


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...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜