72 ajax asp.net-mvc html.actionlink
无论如何,有一个图像充当ajax actionlink?我只能使用文本来使用它.谢谢你的帮助!
Bla*_*rus 67
来自他的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));
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以输入您的aspx文件:
<%= Ajax.ImageActionLink("../../Content/Delete.png", "Delete", "Delete", new { id = item.Id }, new AjaxOptions { Confirm = "Delete contact?", HttpMethod = "Delete", UpdateTargetId = "divContactList" })%>
Run Code Online (Sandbox Code Playgroud)
Nea*_*len 35
这是我发现的最简单的解决方案:
<%= Ajax.ActionLink("[replacethis]", ...).Replace("[replacethis]", "<img src=\"/images/test.gif\" ... />" %>
Run Code Online (Sandbox Code Playgroud)
Replace()调用用于将img标记推送到操作链接.您只需使用"[replaceme]"文本(或任何其他安全文本)作为临时占位符来创建链接.
Arj*_*nbu 31
这是对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)));
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以输入.cshtml文件:
@Ajax.ImageActionLink("../../Content/Delete.png", "Delete", "Delete", new { id = item.Id }, new AjaxOptions { Confirm = "Delete contact?", HttpMethod = "Delete", UpdateTargetId = "divContactList" })
Run Code Online (Sandbox Code Playgroud)
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{ style="border: none;" })
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是创建自己的扩展方法:
ActionLink<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText, object htmlAttributes, LinkOptions options)
Run Code Online (Sandbox Code Playgroud)
最后一个参数是枚举LinkOptions
[Flags]
public enum LinkOptions
{
PlainContent = 0,
EncodeContent = 1,
}
Run Code Online (Sandbox Code Playgroud)
然后你可以按如下方式使用它:
Html.ActionLink<Car>(
c => c.Delete(item.ID), "<span class=\"redC\">X</span>",
new { Class = "none left" },
LinkOptions.PlainContent)
Run Code Online (Sandbox Code Playgroud)
我将在我的博客上发布此解决方案的完整描述:http://fknet.wordpress.com/
简短的回答是不可能的.你可以选择编写自己的扩展方法来拥有一个ImageActionLink,而不是很难做到.或者向actionLink添加一个属性,并将innerhtml替换为image标签.
小智 5
请参阅http://asp.net/mvc上的版本7 Contact Manager Tutorial .Stephen Walther有一个创建Ajax.ActionLink的例子,它是一个图像.
| 归档时间: |
|
| 查看次数: |
51679 次 |
| 最近记录: |