首页 > 代码库 > 【转】Image + ActionLink = Html.ImageActionLink

【转】Image + ActionLink = Html.ImageActionLink

  1 using System;  2 using System.Collections.Generic;  3 using System.Web.Mvc;  4 using System.ComponentModel;  5    6 namespace OPC.Models.Helpers  7 {  8     public static class ImageActionLinkExtension  9     { 10         public static MvcHtmlString ImageActionLink( 11             this HtmlHelper helper, 12             string imageUrl, 13             string altText, 14             string actionName, 15             string controllerName, 16             object routeValues, 17             object linkHtmlAttributes, 18             object imgHtmlAttributes) 19         { 20             var linkAttributes = AnonymousObjectToKeyValue(linkHtmlAttributes); 21             var imgAttributes = AnonymousObjectToKeyValue(imgHtmlAttributes); 22             var imgBuilder = new TagBuilder("img"); 23             imgBuilder.MergeAttribute("src", imageUrl); 24             imgBuilder.MergeAttribute("alt", altText); 25             imgBuilder.MergeAttributes(imgAttributes, true); 26             var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); 27             var linkBuilder = new TagBuilder("a"); 28             linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues)); 29             linkBuilder.MergeAttributes(linkAttributes, true); 30             var text = linkBuilder.ToString(TagRenderMode.StartTag); 31             text += imgBuilder.ToString(TagRenderMode.SelfClosing); 32             text += linkBuilder.ToString(TagRenderMode.EndTag); 33             return MvcHtmlString.Create(text); 34         } 35   36         public static MvcHtmlString ImageActionLink( 37             this HtmlHelper helper, 38             string imageUrl, 39             string altText, 40             string actionName, 41             object routeValues, 42             object imgHtmlAttributes) 43         { 44             var imgAttributes = AnonymousObjectToKeyValue(imgHtmlAttributes); 45             var imgBuilder = new TagBuilder("img"); 46             imgBuilder.MergeAttribute("src", imageUrl); 47             imgBuilder.MergeAttribute("alt", altText); 48             imgBuilder.MergeAttributes(imgAttributes, true); 49             var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); 50             var linkBuilder = new TagBuilder("a"); 51             linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, routeValues)); 52             var text = linkBuilder.ToString(TagRenderMode.StartTag); 53             text += imgBuilder.ToString(TagRenderMode.SelfClosing); 54             text += linkBuilder.ToString(TagRenderMode.EndTag); 55             return MvcHtmlString.Create(text); 56         } 57   58         public static MvcHtmlString ImageActionLink( 59             this HtmlHelper helper, 60             string imageUrl, 61             string altText, 62             string actionName, 63             object routeValues) 64         { 65             var imgBuilder = new TagBuilder("img"); 66             imgBuilder.MergeAttribute("src", imageUrl); 67             imgBuilder.MergeAttribute("alt", altText); 68             var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); 69             var linkBuilder = new TagBuilder("a"); 70             linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, routeValues)); 71             var text = linkBuilder.ToString(TagRenderMode.StartTag); 72             text += imgBuilder.ToString(TagRenderMode.SelfClosing); 73             text += linkBuilder.ToString(TagRenderMode.EndTag); 74             return MvcHtmlString.Create(text); 75         } 76   77         public static MvcHtmlString ImageActionLink( 78             this HtmlHelper helper, 79             string imageUrl, 80             string altText, 81             string actionName) 82         { 83             var imgBuilder = new TagBuilder("img"); 84             imgBuilder.MergeAttribute("src", imageUrl); 85             imgBuilder.MergeAttribute("alt", altText); 86             var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); 87             var linkBuilder = new TagBuilder("a"); 88             linkBuilder.MergeAttribute("href", urlHelper.Action(actionName)); 89             var text = linkBuilder.ToString(TagRenderMode.StartTag); 90             text += imgBuilder.ToString(TagRenderMode.SelfClosing); 91             text += linkBuilder.ToString(TagRenderMode.EndTag); 92             return MvcHtmlString.Create(text); 93         } 94   95         private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject) 96         { 97             var dictionary = new Dictionary<string, object>(); 98             if (anonymousObject != null) 99             {100                 foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))101                 {102                     dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));103                 }104             }105             return dictionary;106         }107     }108 }

 

示例:

@Html.ImageActionLink(Url.Content("~/Content/Images/Icons/edit.png"), "", "Edit", new { id = item.Id }, new { title = "Edit", border = 0, hspace = 2 }) @Html.ImageActionLink(Url.Content("~/Content/Images/Icons/personalDetails.png"), "", "Details", new { id = item.Id }, new { title = "Details", border = 0, hspace = 2 }) @Html.ImageActionLink(Url.Content("~/Content/Images/Icons/delete.png"), "", "Delete", new { id = item.Id }, new { title = "Delete", border = 0, hspace = 2 })

 

 

【转】Image + ActionLink = Html.ImageActionLink