how to generate Html.ActionLink with icon

2019-02-04 02:34发布

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.

5条回答
【Aperson】
2楼-- · 2019-02-04 03:09

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

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

查看更多
做自己的国王
3楼-- · 2019-02-04 03:16

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.

查看更多
虎瘦雄心在
4楼-- · 2019-02-04 03:21
@Html.ActionLink("Edit","Edit","",new { @class= "btn btn-primary" })

Result enter image description here

查看更多
beautiful°
5楼-- · 2019-02-04 03:32

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.

查看更多
霸刀☆藐视天下
6楼-- · 2019-02-04 03:33

Simple as this:

@Html.ActionLink("Title", "Action", null, new {@class="btn btn-info fa fa-pencil" })
查看更多
登录 后发表回答