MVC3自定义标签,@Html.ImageActionLink

View Code
 1 using System; 
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 namespace System.Web.Mvc
7 {
8 public static class MyHelpler
9 {
10 public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
11 {
12 var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
13
14 string imgUrl = urlHelper.Content(imgSrc);
15 TagBuilder imgTagBuilder = new TagBuilder("img");
16 imgTagBuilder.MergeAttribute("src", imgUrl);
17 string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
18
19 string url = urlHelper.Action(actionName);
20
21 TagBuilder tagBuilder = new TagBuilder("a")
22 {
23 InnerHtml = img
24 };
25 tagBuilder.MergeAttribute("href", url);
26
27 return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
28 }
29 public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName,string controllerName,object routeValue=null)
30 {
31 var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
32
33 string imgUrl = urlHelper.Content(imgSrc);
34 TagBuilder imgTagBuilder = new TagBuilder("img");
35 imgTagBuilder.MergeAttribute("src", imgUrl);
36 string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
37
38 string url = urlHelper.Action(actionName, controllerName, routeValue);
39
40 TagBuilder tagBuilder = new TagBuilder("a")
41 {
42 InnerHtml = img
43 };
44 tagBuilder.MergeAttribute("href", url);
45
46 return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
47 }
48 }
49 }

二、在view下使用:

@Html.ActionLinkWithImage(Url.Content("~/Content/images/index_1.gif"), "Index") 
@Html.ActionLinkWithImage(Url.Content("~/Content/images/index_2.gif"), "List","Admin", new { id=1}) 

出处:http://www.bbsmvc.com/mvclearn/thread-30-1-1.html

 

自定义ImgActionLink
 1 public static MvcHtmlString ImageActionLink(
2 this HtmlHelper helper,
3 string imageUrl,
4 string altText,
5 string actionName,
6 string controllerName,
7 object routeValues,
8 object linkHtmlAttributes,
9 object imgHtmlAttributes)
10 {
11 var linkAttributes = AnonymousObjectToKeyValue(linkHtmlAttributes);
12 var imgAttributes = AnonymousObjectToKeyValue(imgHtmlAttributes);
13
14 var imgBuilder = new TagBuilder("img");
15
16 imgBuilder.MergeAttribute("src", imageUrl);
17 imgBuilder.MergeAttribute("alt", altText);
18 imgBuilder.MergeAttributes(imgAttributes, true);
19
20 var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
21
22 var linkBuilder = new TagBuilder("a");
23
24 linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
25 linkBuilder.MergeAttributes(linkAttributes, true);
26
27 var text = linkBuilder.ToString(TagRenderMode.StartTag);
28 text += imgBuilder.ToString(TagRenderMode.SelfClosing);
29 text += linkBuilder.ToString(TagRenderMode.EndTag);
30
31 return MvcHtmlString.Create(text);
32 }
33
34 private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject)
35 {
36 var dictionary = new Dictionary<string, object>();
37
38 if (anonymousObject != null)
39 {
40 foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
41 {
42 dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
43 }
44 }
45
46 return dictionary;
47 }

@Html.ImageActionLink(
   Url.Content("~/Content/Images/" + url),
   alt,
   "Details",
   "Calender",
   new { area = "", day = Model.Date.Day, month = Model.Date.Month, year = Model.Date.Year },
   new { @class = "SelectDay", onClick = string.Format("DoCallbackBookingDetails({0},{1},{2}); return false;", Model.Date.Day, Model.Date.Month, Model.Date.Year) },
   new { style = "border: 0px;" })

posted @ 2012-02-27 18:15  ミ茹此茹此↘  Views(542)  Comments(0Edit  收藏  举报