how to generate Html.ActionLink with icon

2019-02-04 03:08发布

问题:

I'm starting to learn ASP.NET MVC, and have a problem, how generate code with Html.ActionLink like this:

<a href="~/Views/Home/Create.cshtml" class="btn btn-primary">
    <i class="icon-pencil icon-white"></i>
    <span>
        <strong>Create</strong>
    </span>            
</a>

please.

回答1:

Html.ActionLink() only supports plain-text links.

You should use <a href="@Url.Action(...)"> for more-complex links.



回答2:

I wanted to add to SLaks answer.

Using <a href="@Url.Action(...)"> with what user2567619 wanted.

<a href="@Url.Action("Create", "Home")" class="btn btn-primary">
    <i class="icon-pencil icon-white"></i>
    <span>
        <strong>Create</strong>
    </span>            
</a>

I think it's worth mentioning that @Url.Action can take it's parameters like this:

@Url.Action(string actionName, string controllerName) 

Whereas @Html.ActionLink can take it's parameters like this:

@Html.ActionLink(string linkText, string actionName, string controllerName) 

It may be pretty obvious, but I thought it was worth noting.

Edit

As Peck_conyon noted, for both @Url.Action and @Html.ActionLink, these are just one of the ten different overload methods.
For documentation on UrlHelper.Action, look here.
For documentation on LinkEtensions.ActionLink, look here.



回答3:

If it's in the Layout page, you can use this, I think it may help:

    <li>@Html.ActionLink(" Login", "Index", new { Controller = "Login", Area = "Security" }, new { @class = "glyphicon glyphicon-log-in" })</li>

or like this for a action_link:

    <p>
    @Html.ActionLink(" Create New", "Add", "Event", FormMethod.Post, new { @class = "glyphicon glyphicon-plus" })</p>

Hope this helps.



回答4:

@Html.ActionLink("Edit","Edit","",new { @class= "btn btn-primary" })

Result



回答5:

Simple as this:

@Html.ActionLink("Title", "Action", null, new {@class="btn btn-info fa fa-pencil" })