【转】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 })

原文地址:http://kitsula.com/Article/Html.ImageActionLink-extension

时间: 2025-01-02 13:27:01

【转】Image + ActionLink = Html.ImageActionLink的相关文章

Ajax.ActionLink(),增加对img的支持

@Ajax.ActionLink("ActionLink", "Index", "Home", "https", "www", "title", new { id = 1, type = 1 }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "detailsID", Inserti

2016 系统设计第一期 (档案一)MVC a标签 跳转 Html.ActionLink的用法

html: <a class="J_menuItem" href="baidu.com">权限管理</a> cshtml: 原有样式: @Html.ActionLink("权限管理", "UserList", "User", new { style = "color:red", @class = "J_menuItem" }) 再加额外样式:

@html.ActionLink的几种参数格式

一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, 默认控制器为当前页面的控制器,如果当前页面的控制器为Products,则 Html.ActionLink("detail","Detail") 则会生成 <a href="/Products/Detail">all</a> 二

@Html.ActionLink

一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, 默认控制器为当前页面的控制器,如果当前页面的控制器为Products,则 Html.ActionLink("detail","Detail") 则会生成 <a href="/Products/Detail">all</a> 二

asp.net MVC 回顾 Html.ActionLink

在asp.net MVc中想生成一个超链接有很多种方式,通过直接输入<a>.Html.ActionLink.Html.RouteLink等等,今天我们要阐述的就是Html.ActionLink 知识点总结. @Html.ActionLink有10个重载, 示例代码 说明 @Html.ActionLink("LinkText","ActionName") 在生成的超链接中,LinkText指定的文本不可以为Null或空,否则程序会报错 @Html.Acti

Html.ActionLink(转载)

@Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index")@Html.ActionLink("带控制器", "Index", "RsvpForm")@Html.ActionLink("带默认路由参数", "Index", new {page=1 })@Html.A

ASP.NET MVC 学习(Html.ActionLink)

转自 http://www.liuwu.net/post/aspnet-mvc-learning-5-html-actionlink.aspx 一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法,默认控制器为当前页面的控制器,如果当前页面的控制器为Products,则 Html.ActionLink("detail","Detail

ASP.NET MVC HtmlHelper之Html.ActionLink

前言 ActionLink用于生成超链接,方法用于指向Controller的Action. 扩展方法与参数说明 ActionLink扩展方法如下: public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName); public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, strin

Ajax.ActionLink用法

必须要引用的JS库: <script type="text/javascript" src="@Url.StaticFile("/Assets/Content/Scripts/jquery.unobtrusive-ajax.min.js")" charset="utf-8"></script> 异步删除操作回调显示删除成功并刷新页面: @Ajax.ActionLink("删除", &