OrElse
OrElse

Reputation: 9959

How can i add additional html child elements in an MVC ActionLink?

I would like to have the below html markup in MVC

<a href="/Home/ShoppingCart" class="view-cart">
<span data-hover="View Cart"><span>Cart</span></span></a>

but when i use this one

@Html.ActionLink("Cart", "ShoppingCart")

the result is

<a href="/Home/ShoppingCart">Cart</a>

So, how can i add that attributes and extra markups?

The problem is basically with the <spans> involved, which i do not know how to render them within the hyperlink.

Upvotes: 0

Views: 1563

Answers (1)

user3559349
user3559349

Reputation:

You cannot add additional html elements inside the <a> element generated by @Html.ActionLink(). You will need to use

<a href="@Url.Action("ShoppingCart")" class="view-cart">
  <span data-hover="View Cart">
    <span>Cart</span>
  </span>
</a>

If its something you do regularly, you could consider creating you own HtmlHelper extension method which generates the markup for you

Upvotes: 4

Related Questions