Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a span tag in an a tag using Html.ActionLink

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?

like image 358
Brendan Vogt Avatar asked Nov 12 '13 06:11

Brendan Vogt


People also ask

What is HTML ActionLink ()?

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.

Can a span contain a href?

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.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How do I pass an object in ActionLink?

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


1 Answers

Your variant with Url.Action("Index", "Home") is correct and possible. Url.Action returns the url, so it can be used in a href.

like image 139
alexmac Avatar answered Oct 23 '22 17:10

alexmac