如何生成带有图标的 Html.ActionLink

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21465886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:46:34  来源:igfitidea点击:

how to generate Html.ActionLink with icon

htmlasp.net-mvcrazor

提问by Yury Loginov

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

我开始学习 ASP.NET MVC,遇到了一个问题,如何用Html.ActionLink这样的方式生成代码:

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

please.

请。

回答by SLaks

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

Html.ActionLink()只支持纯文本链接。

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

您应该<a href="@Url.Action(...)">用于更复杂的链接。

回答by Trevor Nestman

I wanted to add to SLaks answer.

我想添加到 SLaks 答案中。

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

<a href="@Url.Action(...)">与 user2567619 想要的东西一起使用。

<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.Actioncan take it's parameters like this:

我认为值得一提的是,@Url.Action可以采用这样的参数:

@Url.Action(string actionName, string controllerName) 

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

@Html.ActionLink可以采用这样的参数:

@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.Actionand @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.

正如 Peck_conyon 所指出的,对于@Url.Action@Html.ActionLink,这些只是十种不同的重载方法之一。
有关 的文档UrlHelper.Action,请查看此处
有关 的文档LinkEtensions.ActionLink,请查看此处

回答by Jorge

Simple as this:

就这么简单:

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

回答by Jon A

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:

或者像这样的 action_link:

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

Hope this helps.

希望这可以帮助。

回答by sheeraz ali

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

Resultenter image description here

结果在此处输入图片说明

回答by Santos

This example may help you.

这个例子可能对你有帮助。

@Html.ActionLink(" ", "Controller", new { id = item.Id }, new { @class = "btn
                 btn-success btn-circle fa fa-plus-circle", title = "Confirm" })

this way you will have an icon without the text.

这样你就会有一个没有文字的图标。