How can I put a span element inside ActionLink BUT NOT WITH URL.ACTION?
This:
<li><span>
@Ajax.ActionLink("LinkText", "ControllerName", new AjaxOptions
{
UpdateTargetId = "div",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
LoadingElementId = "progress"
})
</span></li>
generates this:
<li>
<span>
<a href="/Home/ControllerName" data-ajax-update="#scroll"
data-ajax-mode="replace" data-ajax-method="GET"
data-ajax-loading="#progress" data-ajax="true">LinkText</a>
</span>
</li>
But I need something else. How can I create a custom MVC3 ActionLink method that generates this output:
<li>
<a href="/Home/ControllerName" data-ajax-update="#scroll"
data-ajax-mode="replace" data-ajax-method="GET"
data-ajax-loading="#progress" data-ajax="true">
<span>LinkText</span> // this span generated inside <a>
</a>
</li>
I know this is old but I use this quick and dirty way for adding styled button links in my grids. You could add overloads to include route names/etc. as well. Hope this helps someone.
How difficult is it to achieve this by using JQuery?
We can always use
JQuery
and append the<span></span>
once the DOM is loaded.May be not the perfect solution but for developers using jquery and dont want to write html helpers classes as above answer this might be the work around.
I modified markdotnet's answer using Jetbrain's Annotation package to provide intellisense on the actionName parameter. I Also changed the linkText parameter to improve code visualizing; VS will treat it as HTML code, instead of just a simple string.
Example:
Extension class:
Simple answer: you can't. The ActionLink method HTML encodes the link text and there's not much you could do about it (you could open a ticket at Microsoft so that they provide an overload that allows you to do this in ASP.NET MVC vNext).
At the moment you could write a custom html helper that won't encode:
and then:
I modified Darins answer just a bit to be able to accomodate RouteValues.
I know this is an old thread, but you can also do something inline along the lines of:
Its a hack I know, but you could easily write an extension along these lines