在 ASP.NET MVC 的最新 (RC1) 版本中,如何让 Html.ActionLink 呈现为按钮或图像而不是链接?
23 回答
我喜欢这样使用 Url.Action() 和 Url.Content() :
<a href='@Url.Action("MyAction", "MyController")'>
<img src='@Url.Content("~/Content/Images/MyLinkImage.png")' />
</a>
严格来说, Url.Content 仅用于路径并不是您问题答案的一部分。
感谢@BrianLegg 指出这应该使用新的 Razor 视图语法。示例已相应更新。
延迟响应,但您可以保持简单并将 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 */
}
借用帕特里克的回答,我发现我必须这样做:
<button onclick="location.href='@Url.Action("Index", "Users")';return false;">Cancel</button>
避免调用表单的 post 方法。
称我为简单,但我只是这样做:
<a href="<%: Url.Action("ActionName", "ControllerName") %>">
<button>Button Text</button>
</a>
您只需处理超链接突出显示。我们的用户喜欢它:)
使用 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" })
如果您不想使用链接,请使用按钮。您也可以将图像添加到按钮:
<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"执行您的操作而不是提交表单。
简单地说:
<button onclick="@Url.Action("index", "Family", new {familyid = Model.FamilyID })">Cancel</button>
一个迟到的答案,但这就是我如何将我的 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>
}
希望这可以帮助某人
你不能用Html.ActionLink
. 您应该使用Url.RouteUrl
并使用 URL 来构造您想要的元素。
将 Html.ActionLink 变成按钮的一种简单方法(只要您插入了 BootStrap - 您可能已经插入)如下所示:
@Html.ActionLink("Button text", "ActionName", "ControllerName", new { @class = "btn btn-primary" })
<button onclick="location.href='@Url.Action("NewCustomer", "Customers")'">Checkout >></button>
<li><a href="@Url.Action( "View", "Controller" )"><i class='fa fa-user'></i><span>Users View</span></a></li>
显示带有链接的图标
按照 Mehrdad 所说的去做 - 或者使用HtmlHelper
Stephen Walther在这里描述的扩展方法中的 url 帮助程序,并制作您自己的扩展方法,该方法可用于呈现您的所有链接。
然后很容易将所有链接呈现为按钮/锚或任何您喜欢的方式 - 最重要的是,当您发现您实际上更喜欢其他方式制作链接时,您可以改变主意。
你可以创建自己的扩展方法
看看我的实现
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
)
对于 Material Design Lite 和 MVC:
<a class="mdl-navigation__link" href='@Url.Action("MyAction", "MyController")'>Link Name</a>
@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?');" />
}
关于如何创建显示为图像的链接似乎有很多解决方案,但没有一个解决方案使它看起来像一个按钮。
我发现只有一个好方法可以做到这一点。它有点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();
}
每次添加需要看起来像按钮的链接时,这样做可能会让人头疼,但它确实实现了预期的结果。
使用 FORMACTION
<input type="submit" value="Delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
Url.Action()
将为您提供大多数重载的裸 URL Html.ActionLink
,但我认为该URL-from-lambda
功能仅在目前可用Html.ActionLink
。希望他们会Url.Action
在某个时候添加类似的重载。
刚刚找到了这个扩展来做到这一点 - 简单而有效。
我这样做的方式是分别拥有 actionLink 和图像。将 actionlink 图像设置为隐藏,然后添加一个 jQuery 触发器调用。这更像是一种解决方法。
'<%= Html.ActionLink("Button Name", "Index", null, new { @class="yourclassname" }) %>'
<img id="yourImage" src="myImage.jpg" />
触发示例:
$("#yourImage").click(function () {
$('.yourclassname').trigger('click');
});
这就是我在没有脚本的情况下做到的:
@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>
}