asp.net-mvc Html.ActionLink 作为按钮或图像,而不是链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/596444/
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-09-07 23:22:03  来源:igfitidea点击:

Html.ActionLink as a button or an image, not a link

asp.net-mvcactionlink

提问by Ryan Lundy

In the latest (RC1) release of ASP.NET MVC, how do I get Html.ActionLink to render as a button or an image instead of a link?

在 ASP.NET MVC 的最新 (RC1) 版本中,如何让 Html.ActionLink 呈现为按钮或图像而不是链接?

回答by jslatts

I like to use Url.Action() and Url.Content() like this:

我喜欢像这样使用 Url.Action() 和 Url.Content() :

<a href='@Url.Action("MyAction", "MyController")'>
    <img src='@Url.Content("~/Content/Images/MyLinkImage.png")' />
</a>

Strictly speaking, the Url.Content is only needed for pathing is not really part of the answer to your question.

严格来说,仅用于路径的 Url.Content 并不是您问题答案的真正组成部分。

Thanks to @BrianLegg for pointing out that this should use the new Razor view syntax. Example has been updated accordingly.

感谢@BrianLegg 指出这应该使用新的 Razor 视图语法。示例已相应更新。

回答by Mark

Late response but you could just keep it simple and apply a CSS class to the htmlAttributes object.

迟到的响应,但您可以保持简单并将 CSS 类应用于 htmlAttributes 对象。

<%= Html.ActionLink("Button Name", "Index", null, new { @class="classname" }) %>

and then create a class in your stylesheet

然后在样式表中创建一个类

a.classname
{
    background: url(../Images/image.gif) no-repeat top left;
     display: block;
     width: 150px;
     height: 150px;
     text-indent: -9999px; /* hides the link text */
}

回答by DevDave

Borrowing from Patrick's answer, I found that I had to do this:

借用帕特里克的回答,我发现我必须这样做:

<button onclick="location.href='@Url.Action("Index", "Users")';return false;">Cancel</button>

to avoid calling the form's post method.

避免调用表单的 post 方法。

回答by agAus

Call me simplistic, but I just do:

叫我简单化,但我只是这样做:

<a href="<%: Url.Action("ActionName", "ControllerName") %>">
    <button>Button Text</button>
</a>

And you just take care of the hyperlink highlight. Our users love it :)

您只需处理超链接突出显示。我们的用户喜欢它:)

回答by Cameron Forward

Using bootstrap this is the shortest and cleanest approach to create a link to a controller action that appears as a dynamic button:

使用引导程序,这是创建指向控制器操作的链接的最短且最干净的方法,该链接显示为动态按钮:

<a href='@Url.Action("Action", "Controller")' class="btn btn-primary">Click Me</a>

Or to use Html helpers:

或者使用 Html 助手:

@Html.ActionLink("Click Me", "Action", "Controller", new { @class = "btn btn-primary" })

回答by Patrick Kan

Just simply :

简单地说:

<button onclick="@Url.Action("index", "Family", new {familyid = Model.FamilyID })">Cancel</button>

回答by Emperor 2052

A late answer but this is how I make my ActionLink into a button. We're using Bootstrap in our project as it makes it convenient. Never mind the @T since its only an translator we're using.

一个迟到的答案,但这就是我将 ActionLink 变成按钮的方式。我们在项目中使用 Bootstrap,因为它很方便。别介意@T,因为它只是我们使用的翻译器。

@Html.Actionlink("Some_button_text", "ActionMethod", "Controller", "Optional parameter", "html_code_you_want_to_apply_to_the_actionlink");

The above gives a link like this and it looks as the picture below:

上面给出了一个这样的链接,它看起来像下图:

localhost:XXXXX/Firms/AddAffiliation/F0500

picture demonstrating button with bootstrap

带引导程序的图片演示按钮

In my view:

在我看来:

@using (Html.BeginForm())
{
<div class="section-header">
    <div class="title">
        @T("Admin.Users.Users")
    </div>
    <div class="addAffiliation">
        <p />
        @Html.ActionLink("" + @T("Admin.Users.AddAffiliation"), "AddAffiliation", "Firms", new { id = (string)@WorkContext.CurrentFirm.ExternalId }, new { @class="btn btn-primary" })
    </div>
</div>

}

Hope this helps somebody

希望这可以帮助某人

回答by Amir Chatrbahr

if you don't want to use a link, use button. you can add image to button as well:

如果您不想使用链接,请使用按钮。您也可以将图像添加到按钮:

<button type="button" onclick="location.href='@Url.Action("Create", "Company")'" >
   Create New
   <img alt="New" title="New" src="~/Images/Button/plus.png">
</button>

type="button"performs your action instead of submitting form.

type="button"执行您的操作而不是提交表单。

回答by Mehrdad Afshari

You can't do this with Html.ActionLink. You should use Url.RouteUrland use the URL to construct the element you want.

你不能用Html.ActionLink. 您应该使用Url.RouteUrlURL 来构建您想要的元素。

回答by Jiri HWeb Horalek

<button onclick="location.href='@Url.Action("NewCustomer", "Customers")'">Checkout >></button>

<button onclick="location.href='@Url.Action("NewCustomer", "Customers")'">Checkout >></button>