using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc; namespace MyEF.ExtentionHtmlHelper
{
public static class ImageHelper
{
//说明:因为C#3.0的扩充方法是一种特殊的静态方法,因此在开发Html辅助方法时必须声明为
//静态方法(static method)并放在一个静态的类别里面!此外自定义的Html方法还有一个重点,那就是从Html辅助方法里,回传的类型可以使简单的String字符串类型,也可以是System.Web.MvcHtmlString类别,差别在于,通过rasor输出的Html预设就会对所有输出的Html编码动作,所以如果回传string字符串类型,其输出的内容将会被Html编码后输出,如果你从Html辅助方法回传MvcHtmlString类型的话,如果内容包含标签数据,那么就会原封不动的输出Html标签,以我们的这个范例来说,必须回传MvcHtmlString类别,代码范例如下:
public static MvcHtmlString HtmlImg(this HtmlHelper helper, string url, string alternateText, string title)
{
return MvcHtmlString.Create(string.Format("<img src='{0} alt={1}' title='{2}' />", url, alternateText, title));
}
public static MvcHtmlString HtmlImg(this HtmlHelper helper, string linkUrl, string target, string imgUrl, string alterText, string title)
{
StringBuilder sb = new StringBuilder();
sb.Append("<a href=\"");
sb.Append(linkUrl);
sb.Append("\" target=\"");
sb.Append(target);
sb.Append("\">");
sb.Append("<img src=\"");
sb.Append(imgUrl);
sb.Append("\"");
sb.Append("\" alt=\"");
sb.Append(alterText);
sb.Append("\"");
sb.Append("\" title=\"");
sb.Append(title);
sb.Append("\" /></a>");
return MvcHtmlString.Create(sb.ToString());
}
}
}

  这种利用StringBuilder组子串的方式没有任何弹性,ASP.NET MVC专门为了产生HTML标签的需求而设计了一个TagBuilder类别,能够以更物件化的方式产生HTML标签,接着来改写上述范例,以TagBuilder实例如下:

 public static MvcHtmlString ImageLink(this HtmlHelper helper,string actionName,string imgUrl, string alternateText,object routeValues,object linkHtmlAttributes,object imageHtmlAttributes)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.Action(actionName, routeValues);
//建立链接
var linkTagBuilder = new TagBuilder("a");
linkTagBuilder.MergeAttribute("href", url);
linkTagBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes)); //建立图片
var imageTagBuilder = new TagBuilder("img");
imageTagBuilder.MergeAttribute("src", urlHelper.Content(imgUrl));
imageTagBuilder.MergeAttribute("alt",alternateText);
imageTagBuilder.MergeAttribute("title",alternateText);
imageTagBuilder.MergeAttributes(new ViewDataDictionary(imageHtmlAttributes)); //将图片加之连接中
linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);
return MvcHtmlString.Create(linkTagBuilder.ToString());
}

  

  /// <summary>
/// 工作类型HtmlHelper
/// </summary>
/// <param name="helper"></param>
/// <param name="natureWork"></param>
/// <returns></returns>
public static MvcHtmlString ExtendNatureWork(this HtmlHelper helper, string natureWork) {
var sb = new System.Text.StringBuilder();
var flag = natureWork.IsNull();
var arr = natureWork.IsNull() ? new string[1] : natureWork.Split(',');
foreach (var item in Macrosage.Utility.TypeHelper.ToDictionary(Macrosage.Utility.EnumHelper.JobType.实习).Where(x => x.Key != "3")) {
if (item.Key == "1") {
sb.AppendFormat("<span {0} data-id=\"{2}\"><em class=\"icon\"></em>{1}</span>", arr.Contains(item.Key) || flag ? "class='current'" : string.Empty, item.Value, item.Key);
}
else if (item.Key == "2") {
sb.AppendFormat("<span {0} data-id=\"{2}\"><em class=\"icon\"></em>{1}</span>", arr.Contains(item.Key) ? "class='current'" : string.Empty, item.Value, item.Key);
}
else {
sb.AppendFormat("<span {0} data-id=\"{2}\"><em class=\"icon\"></em>{1}</span>", arr.Contains(item.Key) ? "class='fix_noboder current'" : string.Empty, item.Value, item.Key);
}
}
return MvcHtmlString.Create(sb.ToString());
}

  

        public static MvcHtmlString ExtendMy(this HtmlHelper helper, string welfare) {
var stringBulider = new StringBuilder();
var arr = welfare.IsNull() ? new string[1] : welfare.Split(",");
var data = EnumHelper.Welfare.Where(x => x.Key != "9");
foreach (var item in data) {
var index = data.ToList().IndexOf(item) + 1;
stringBulider.AppendFormat("<span class=\"{0} {2}\" data-id=\"{3}\"><em class=\"icon\"></em>{1}</span>", arr.Contains(item.Value) ? "current" : string.Empty, item.Value, index.Equals(data.Count()) ? "fix_noboder" : string.Empty, item.Key);
}
return MvcHtmlString.Create(stringBulider.ToString());
}

  

04-30 02:17