今天研究Ajax.ActionLink之使用方法,但需以Image顯示Link之方式,
而ASP.NET MVC RC2並沒有支援,故Google一下,發現有人已遇到相同問題,
且有解法故將其記錄下來:
1. 實作一個ImageActionLinkHelper類別:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
namespace IPRO.Mvc.Html
{
    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.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
        }
    }
}
2.ASP.NET 頁面.aspx
<%= Ajax.ImageActionLink(Url.Content("~/Content/img/trashcan_icon.gif"), Resources.ContactPersonDelete, "Delete", new { Person_id = item.Person_id },new AjaxOptions { Confirm = "確認", HttpMethod = "Post", OnSuccess = "myReload" })%>
轉自:http://stackoverflow.com/questions/341649/asp-net-mvc-ajax-actionlink-with-image
 
沒有留言:
張貼留言