Konrad Viltersten Konrad Viltersten - 7 months ago 47
HTML Question

How to invoke an MVC action method from a href in an anchor?

I'm using the markup as follows.

@Html.ActionLink(@User.Identity.Name, "LogOut", "Account")


Now, I need to add a span inside the anchor because I want to use glyphs from Bootstrap. As far my googlearch went, there's no way to specify it using the helper above. So, I've redesigned it to explicit HTML as follows.

<a href="~/Account/LogOut">
<span class="glyphicon glyphicon-log-out"></span>
<span>@Global.LogOut @User.Identity.Name</span>
</a>


It works but the link isn't always targeting the same address as the first example does. It's because I've got en/ or se/ etc. for language first. When routing based on MVC, the language prefix stays in place but it gets lost when specifying the URL explicitly.

How can I specify a call to the specific action method?

Answer

Try:

<a href="@Url.Action("LogOut", "Account")">
  <span class="glyphicon glyphicon-log-out"></span>
  <span>@Global.LogOut @User.Identity.Name</span>
</a>