using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Text;namespace System.Web.Mvc.Html{ public static class HtmlExtensions { //用 table 进行包裹 /* public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string inputName, IEnumerable selectList, Position position = Position.Horizontal) { return GenerateHtml(inputName, "checkbox", selectList, position); } public static MvcHtmlString CheckBoxListFor (this HtmlHelper htmlHelper, Expression > expression, Position position = Position.Horizontal) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); string inputName = ExpressionHelper.GetExpressionText(expression); IEnumerable selectList = htmlHelper.ViewData[inputName] as IEnumerable ; IEnumerable modelItems = metadata.Model as IEnumerable ; if (selectList == null) { selectList = new List (); } if (modelItems != null) { selectList.ForEach(a => { modelItems.Contains(a.Value).WhereTrue(() => a.Selected = true); }); } return GenerateHtml(inputName, "checkbox", selectList, position); } public static MvcHtmlString RadioButtonList(this HtmlHelper helper, string inputName, IEnumerable selectList, Position position = Position.Horizontal) { return GenerateHtml(inputName, "radio", selectList, position); } public static MvcHtmlString RadioButtonListFor (this HtmlHelper htmlHelper, Expression > expression, Position position = Position.Horizontal) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData); //string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(inputName); string inputName = ExpressionHelper.GetExpressionText(expression); IEnumerable selectList = htmlHelper.ViewData[inputName] as IEnumerable ; if (selectList == null) { selectList = new List (); } if (metadata.Model != null) { selectList.ForEach(a => (a.Value == metadata.Model.ToString()).WhereTrue(() => a.Selected = true)); } return GenerateHtml(inputName, "radio", selectList, position); } public static MvcHtmlString GenerateHtml(string inputName, string inputType, IEnumerable selectList, Position position) { TagBuilder table = new TagBuilder("table"); int i = 0; if (position == Position.Horizontal) { TagBuilder tr = new TagBuilder("tr"); foreach (var item in selectList) { i++; string inputId = string.Format("{0}_{1}", inputName, i); TagBuilder td = new TagBuilder("td"); td.InnerHtml = GenerateRadioHtml(inputName, inputId, inputType, item.Value, item.Text, item.Selected); tr.InnerHtml += td.ToString(); } table.InnerHtml = tr.ToString(); } else { foreach (var item in selectList) { TagBuilder tr = new TagBuilder("tr"); i++; string inputId = string.Format("{0}_{1}", inputName, i); TagBuilder td = new TagBuilder("td"); td.InnerHtml = GenerateRadioHtml(inputName, inputId, inputType, item.Value, item.Text, item.Selected); tr.InnerHtml = td.ToString(); table.InnerHtml += tr.ToString(); } } return new MvcHtmlString(table.ToString()); } private static string GenerateRadioHtml(string inputName, string inputId, string inputType, string inputValue, string labelText, bool isChecked) { StringBuilder sb = new StringBuilder(); TagBuilder label = new TagBuilder("label"); label.MergeAttribute("for", inputId); label.SetInnerText(labelText); TagBuilder input = new TagBuilder("input"); input.GenerateId(inputId); input.MergeAttribute("name", inputName); input.MergeAttribute("type", inputType); input.MergeAttribute("value", inputValue); if (isChecked) { input.MergeAttribute("checked", "checked"); } sb.AppendLine(input.ToString(TagRenderMode.SelfClosing)); sb.AppendLine(label.ToString()); return sb.ToString(); } */ //生成格式
using System.Text;namespace System.Web.Mvc.Html{ public static class HtmlHelperExtensions { public static HtmlString SimplePagination(this HtmlHelper htmlHelper, int pageIndex, int pageSize, int totalCount, object queryString = null) { var strQueryString = queryString.GetQueryString(); pageSize = pageSize <= 0 ? 3 : pageSize; var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数 var output = new StringBuilder(); if (totalPages > 1) { var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; if (pageIndex > 1) { //处理首页连接 output.AppendFormat("首页 ", redirectTo, pageSize, strQueryString); } else { output.Append("首页 "); } if (pageIndex > 1) { //处理上一页的连接 output.AppendFormat("上一页 ", redirectTo, pageIndex - 1, pageSize, strQueryString); } else { output.Append("上一页 "); } int intervalInt = 5; int begin = Math.Max((pageIndex - intervalInt), 1); //int end = Math.Min(totalPages, begin + (intervalInt * 2)); int end = Math.Min(totalPages, ((intervalInt * 2) + (pageIndex - intervalInt))); for (int i = begin; i <= end; i++) { if (i == pageIndex) { //当前页处理 output.AppendFormat("{4} ", redirectTo, pageIndex, pageSize, strQueryString, pageIndex); } else { //一般页处理 output.AppendFormat("{4} ", redirectTo, i, pageSize, strQueryString, i); } } if (pageIndex < totalPages) { //处理下一页的链接 output.AppendFormat("下一页 ", redirectTo, pageIndex + 1, pageSize, strQueryString); } else { output.Append("下一页 "); } if (pageIndex < totalPages) { output.AppendFormat("尾页 ", redirectTo, totalPages, pageSize, strQueryString); } else { output.Append("尾页"); } } output.AppendFormat("第{0}页 / 共{1}页", pageIndex, totalPages);//这个统计加不加都行 return new HtmlString(output.ToString()); } public static string GetQueryString(this object queryString) { if (queryString == null) { return ""; } Type type = queryString.GetType(); var sb = new StringBuilder(); foreach (var item in type.GetProperties()) { sb.AppendFormat("&{0}={1}", item.Name, item.GetValue(queryString, null)); } return sb.ToString(); } public static string GetHtmlAttributes(object htmlAttributes) { if (htmlAttributes == null) { return ""; } Type type = htmlAttributes.GetType(); var sb = new StringBuilder(); foreach (var item in type.GetProperties()) { sb.AppendFormat("{0}=\"{1}\" ", item.Name, item.GetValue(htmlAttributes, null)); } return sb.ToString(); } }}
using System.Linq.Expressions;namespace System.Web.Mvc.Html{ public static class DisplayDescriptionExtensions { #region - DisplayDescription - /// /// 模型描述信息 /// /// /// /// public static MvcHtmlString DisplayDescription(this HtmlHelper htmlHelper, string name) { ModelMetadata modelMetadata = ModelMetadata.FromStringExpression(name, htmlHelper.ViewData); return MvcHtmlString.Create(modelMetadata.Description); } /// /// 模型描述信息 /// /// /// /// /// /// public static MvcHtmlString DisplayDescriptionFor (this HtmlHelper htmlHelper, Expression > expression) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); return MvcHtmlString.Create(modelMetadata.Description); } #endregion }}