我认为更好的方法是在你的帮助文件夹中使用这些重载为它创建一个扩展方法,然后在你的视图中使用它。只取决于个人喜好
public static class ImageActionLinkHelper
{
public static string ImageActionLink(this HtmlHelper helper, string ImageUrl, string altText, string actionName, object routeValues)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", ImageUrl);
builder.MergeAttribute("alt", altText);
builder.MergeAttribute("title", altText);
var link = helper.ActionLink("[replaceme]", actionName, routeValues, new { @class = "imgicon" });
return link.ToString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
}
public static string ImageActionLink(this HtmlHelper helper, string ImageUrl, string altText, string actionName, object routeValues, string Id, string display)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", ImageUrl);
builder.MergeAttribute("alt", altText);
builder.MergeAttribute("title", altText);
var link = helper.ActionLink("[replaceme]", actionName, routeValues, new { @class = "imgicon", id = Id, style = display });
return link.ToString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
}使用它
@Html.ImageActionLink("../../Content/images/edit.png", "Edit", "Edit", new { id = item.UserId})