无论如何,有一个图像充当ajax actionlink?我只能使用文本来使用它.谢谢你的帮助!
来自他的Contact manger 项目的 Stephen Walthe
public static class ImageActionLinkHelper { public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions) { var builder = new TagBuilder("img"); builder.MergeAttribute("src", imageUrl); builder.MergeAttribute("alt", altText); var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions); return link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)); } }
您现在可以输入您的aspx文件:
<%= Ajax.ImageActionLink("../../Content/Delete.png", "Delete", "Delete", new { id = item.Id }, new AjaxOptions { Confirm = "Delete contact?", HttpMethod = "Delete", UpdateTargetId = "divContactList" })%>
这是我发现的最简单的解决方案:
<%= Ajax.ActionLink("[replacethis]", ...).Replace("[replacethis]", "" %>
Replace()调用用于将img标记推送到操作链接.您只需使用"[replaceme]"文本(或任何其他安全文本)作为临时占位符来创建链接.
这是对Black Horus回答的Razor/MVC 3(及更高版本)更新:
using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; public static class ImageActionLinkHelper { public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes = null) { var builder = new TagBuilder("img"); builder.MergeAttribute("src", imageUrl); builder.MergeAttribute("alt", altText); builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString(); return MvcHtmlString.Create(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing))); } }
您现在可以输入.cshtml文件:
@Ajax.ImageActionLink("../../Content/Delete.png", "Delete", "Delete", new { id = item.Id }, new AjaxOptions { Confirm = "Delete contact?", HttpMethod = "Delete", UpdateTargetId = "divContactList" })
2013年10月31日:更新了一个额外的参数,以允许为图像元素设置其他HTML属性.用法:
@Ajax.ImageActionLink("../../Content/Delete.png", "Delete", "Delete", new { id = item.Id }, new AjaxOptions { Confirm = "Delete contact?", HttpMethod = "Delete", UpdateTargetId = "divContactList" }, new{ })
另一种解决方案是创建自己的扩展方法:
ActionLink(this HtmlHelper helper, Expression > action, string linkText, object htmlAttributes, LinkOptions options)
最后一个参数是枚举LinkOptions
[Flags] public enum LinkOptions { PlainContent = 0, EncodeContent = 1, }
然后你可以按如下方式使用它:
Html.ActionLink( c => c.Delete(item.ID), "X", new { Class = "none left" }, LinkOptions.PlainContent)
我将在我的博客上发布此解决方案的完整描述:http://fknet.wordpress.com/
简短的回答是不可能的.你可以选择编写自己的扩展方法来拥有一个ImageActionLink,而不是很难做到.或者向actionLink添加一个属性,并将innerhtml替换为image标签.
请参阅http://asp.net/mvc上的版本7 Contact Manager Tutorial .Stephen Walther有一个创建Ajax.ActionLink的例子,它是一个图像.