How do you add a span tag
inside of an a tag
using Html.ActionLink
? Is something like this at all possible?
I am using ASP.NET MVC 4
together with Twitter Bootstrap version 3
.
I have a drop down menu and the HTML looks like this:
<ul class="nav navbar-nav">
<li class="@(Model.Equals("Home") ? "active" : "")">
<a href="#">
<span class="glyphicon glyphicon-home"></span> Home
</a>
</li>
</ul>
I was wanting to do the link like this:
<li>@Html.ActionLink("Home", "Index", "Home")</li>
But there is a span tag in the mix of things which makes it a bit more complex.
I can probably also use the following:
<a href="@Url.Action("Index", "Home")">
<span class="glyphicon glyphicon-home"></span> Home
</a>
Curious to know if the above is possible?
ActionLink(HtmlHelper, String, String) Returns an anchor element (a element) for the specified link text and action. ActionLink(HtmlHelper, String, String, Object) Returns an anchor element (a element) for the specified link text, action, and route values.
HTML is a very flexible scripting language that allows you to add a lot of things that are not defined in the language, which then will be simply ignored. So according to that, adding an href attribute on an span element is valid, but will not work as a real href attribute and will be simply ignored.
Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).
Your variant with Url.Action("Index", "Home")
is correct and possible. Url.Action
returns the url, so it can be used in a href.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With