335

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

4

23 回答 23

356

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

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

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

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

于 2010-05-05T14:42:43.287 回答
334

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

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

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

a.classname
{
    background: url(../Images/image.gif) no-repeat top left;
     display: block;
     width: 150px;
     height: 150px;
     text-indent: -9999px; /* hides the link text */
}
于 2009-03-20T20:23:22.313 回答
96

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

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

避免调用表单的 post 方法。

于 2012-09-20T13:43:20.533 回答
51

称我为简单,但我只是这样做:

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

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

于 2011-09-27T04:29:56.967 回答
23

使用 bootstrap 这是创建链接到显示为动态按钮的控制器操作的最短和最干净的方法:

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

或者使用 Html 助手:

@Html.ActionLink("Click Me", "Action", "Controller", new { @class = "btn btn-primary" })
于 2017-03-28T04:10:47.787 回答
16

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

<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"执行您的操作而不是提交表单。

于 2014-10-02T02:41:04.553 回答
15

简单地说:

<button onclick="@Url.Action("index", "Family", new {familyid = Model.FamilyID })">Cancel</button>
于 2012-01-28T17:52:45.080 回答
15

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

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

上面给出了一个这样的链接,如下图所示:

localhost:XXXXX/Firms/AddAffiliation/F0500

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

在我看来:

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

}

希望这可以帮助某人

于 2014-01-09T13:46:22.700 回答
12

你不能用Html.ActionLink. 您应该使用Url.RouteUrl并使用 URL 来构造您想要的元素。

于 2009-02-27T20:12:52.313 回答
11

将 Html.ActionLink 变成按钮的一种简单方法(只要您插入了 BootStrap - 您可能已经插入)如下所示:

@Html.ActionLink("Button text", "ActionName", "ControllerName", new { @class = "btn btn-primary" })
于 2017-06-30T14:46:58.163 回答
9

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

于 2012-03-05T14:04:29.387 回答
8

甚至后来的回应,但我刚刚遇到了类似的问题,最终编写了我自己的图像链接 HtmlHelper 扩展

您可以在上面链接中的我的博客上找到它的实现。

只是添加以防有人正在寻找实现。

于 2009-06-15T18:42:31.997 回答
5
<li><a href="@Url.Action(  "View", "Controller" )"><i class='fa fa-user'></i><span>Users View</span></a></li>

显示带有链接的图标

于 2015-05-26T12:35:31.820 回答
4

按照 Mehrdad 所说的去做 - 或者使用HtmlHelperStephen Walther在这里描述的扩展方法中的 url 帮助程序,并制作您自己的扩展方法,该方法可用于呈现您的所有链接。

然后很容易将所有链接呈现为按钮/锚或任何您喜欢的方式 - 最重要的是,当您发现您实际上更喜欢其他方式制作链接时,您可以改变主意。

于 2009-02-27T20:25:05.547 回答
3

你可以创建自己的扩展方法
看看我的实现

public static class HtmlHelperExtensions
{
    public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt, object htmlAttributesForAnchor, object htmlAttributesForImage)
    {
        var url = new UrlHelper(html.ViewContext.RequestContext);

        // build the <img> tag
        var imgBuilder = new TagBuilder("img");
        imgBuilder.MergeAttribute("src", url.Content(imagePath));
        imgBuilder.MergeAttribute("alt", alt);
        imgBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributesForImage));
        string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

        // build the <a> tag
        var anchorBuilder = new TagBuilder("a");
        anchorBuilder.MergeAttribute("href", action != null ? url.Action(action, routeValues) : "#");
        anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
        anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributesForAnchor));

        string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
        return MvcHtmlString.Create(anchorHtml);
    }
}

然后在你的视图中使用它看看我的电话

 @Html.ActionImage(null, null, "../../Content/img/Button-Delete-icon.png", Resource_en.Delete,
               new{//htmlAttributesForAnchor
                   href = "#",
                   data_toggle = "modal",
                   data_target = "#confirm-delete",
                   data_id = user.ID,
                   data_name = user.Name,
                   data_usertype = user.UserTypeID
               }, new{ style = "margin-top: 24px"}//htmlAttributesForImage
                    )
于 2016-08-16T16:39:52.367 回答
2

对于 Material Design Lite 和 MVC:

<a class="mdl-navigation__link" href='@Url.Action("MyAction", "MyController")'>Link Name</a>
于 2018-10-30T17:42:33.617 回答
1
@using (Html.BeginForm("DeleteMember", "Member", new { id = Model.MemberID }))
    {
        <input type="submit" value="Delete Member" onclick = "return confirm('Are you sure you want to delete the member?');" />
    }
于 2012-01-11T03:35:03.270 回答
1

关于如何创建显示为图像的链接似乎有很多解决方案,但没有一个解决方案使它看起来像一个按钮。

我发现只有一个好方法可以做到这一点。它有点hacky,但它有效。

您需要做的是创建一个按钮和一个单独的操作链接。使用 css 使操作链接不可见。当您单击按钮时,它可以触发操作链接的单击事件。

<input type="button" value="Search" onclick="Search()" />
 @Ajax.ActionLink("Search", "ActionName", null, new AjaxOptions {}, new { id = "SearchLink", style="display:none;" })

function Search(){
    $("#SearchLink").click();
 }

每次添加需要看起来像按钮的链接时,这样做可能会让人头疼,但它确实实现了预期的结果。

于 2013-10-11T19:35:11.003 回答
1

使用 FORMACTION

<input type="submit" value="Delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
于 2015-12-07T11:25:53.847 回答
0

Url.Action()将为您提供大多数重载的裸 URL Html.ActionLink,但我认为该URL-from-lambda功能仅在目前可用Html.ActionLink。希望他们会Url.Action在某个时候添加类似的重载。

于 2010-11-29T10:35:03.123 回答
0

刚刚找到了这个扩展来做到这一点 - 简单而有效。

于 2012-01-06T12:34:18.377 回答
0

我这样做的方式是分别拥有 actionLink 和图像。将 actionlink 图像设置为隐藏,然后添加一个 jQuery 触发器调用。这更像是一种解决方法。

'<%= Html.ActionLink("Button Name", "Index", null, new { @class="yourclassname" }) %>'
<img id="yourImage" src="myImage.jpg" />

触发示例:

$("#yourImage").click(function () {
  $('.yourclassname').trigger('click');
});
于 2012-05-03T13:58:53.813 回答
0

这就是我在没有脚本的情况下做到的:

@using (Html.BeginForm("Action", "Controller", FormMethod.Get))
{
    <button type="submit" 
            class="btn btn-default" 
            title="Action description">Button Label</button>
}

相同,但带有参数和确认对话框:

@using (Html.BeginForm("Action", "Controller", 
        new { paramName = paramValue }, 
        FormMethod.Get, 
        new { onsubmit = "return confirm('Are you sure?');" }))
{
    <button type="submit" 
            class="btn btn-default" 
            title="Action description">Button Label</button>
}
于 2016-06-19T22:38:32.630 回答