如何在ActionLink按钮中将图像代替文本:
@Html.ActionLink("Edit-link", "Edit", new { id=use.userID })
那么如何将文本“编辑链接”更改为图像?
感谢任何想法。
这样做:
<a href="@Url.Action("Edit")" id="@use.userID">
<img src="@Url.Content("~/images/someimage.png")" />
</a>
或通过使用其他替代来传递action和controller名称:
<a href="@Url.Action("Edit","Controller")" id="@use.userID">
<img src="@Url.Content("~/images/someimage.png")" />
</a>
您还可以创建一个custom Html Helper,并且可以在应用程序的任何视图中重复使用它:
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());
}
}
}
并在视图中使用它:
@using MyApplication.Helpers;
@Html.ImageActionLink("LinkText","ActionName","ControllerName",null,null,"~/images/untitled.png")
<a href="/ControllerName/ActionName">
<img src="/images/untitled.png">
</a>
尝试此代码:
@Html.Raw(@Html.ActionLink("Edit-link","Edit", new { id=use.userID }).ToHtmlString().Replace("Edit-link", "<img src=\"/Contents/img/logo.png\" ... />"))
或
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9zZFczYi5qcGcifQ==” alt =“在此处输入图像描述”>
尝试此代码,将有助于在编辑链接上添加图像