asp.net-mvc ActionLink MVC 中的图像按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23535704/
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
Image button in ActionLink MVC
提问by Daniel K Rudolf_mag
How to put image instead text in ActionLink button:
如何在 ActionLink 按钮中放置图像而不是文本:
@Html.ActionLink("Edit-link", "Edit", new { id=use.userID })
So how to change text "Edit-link" to image?
那么如何将文本“编辑链接”更改为图像?
Thanks for any idea.
感谢您的任何想法。
回答by Ehsan Sajjad
do like this:
这样做:
<a href="@Url.Action("Edit")" id="@use.userID">
<img src="@Url.Content("~/images/someimage.png")" />
</a>
or pass both actionand controllername by using other override:
或使用其他覆盖传递操作和控制器名称:
<a href="@Url.Action("Edit","Controller")" id="@use.userID">
<img src="@Url.Content("~/images/someimage.png")" />
</a>
UPDATE:
更新:
You can also create a custom Html Helper, and can reuse it in any View in application:
您还可以创建自定义 Html Helper,并且可以在应用程序中的任何 View 中重用它:
namespace MyApplication.Helpers
{
public static class CustomHtmlHelepers
{
public static IHtmlString ImageActionLink(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, object htmlAttributes,string imageSrc)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var img = new TagBuilder("img");
img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
var anchor = new TagBuilder("a") { InnerHtml = img.ToString(TagRenderMode.SelfClosing) };
anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(anchor.ToString());
}
}
}
and use it in View:
并在视图中使用它:
@using MyApplication.Helpers;
@Html.ImageActionLink("LinkText","ActionName","ControllerName",null,null,"~/images/untitled.png")
Output HTML:
输出 HTML:
<a href="/ControllerName/ActionName">
<img src="/images/untitled.png">
</a>
回答by Kumar Manish
Try this code :
试试这个代码:
@Html.Raw(@Html.ActionLink("Edit-link","Edit", new { id=use.userID }).ToHtmlString().Replace("Edit-link", "<img src=\"/Contents/img/logo.png\" ... />"))
or
或者


回答by Aruna Biradar
<a href="@Url.Action("Edit Link","Edit",new {id = item.EId })">
<img src="@Url.Content("~/img/iconfinder_new-24_103173.png")" style="height:20px;width:20px;color:blue" title="Edit" />
</a>
Try this code. It will add an image linked to the Edit()action with the idset.
试试这个代码。它将添加一个图像链接到Edit()该id集合的动作。

