博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC 自定义分面控件
阅读量:6990 次
发布时间:2019-06-27

本文共 17009 字,大约阅读时间需要 56 分钟。

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 }}

  

转载于:https://www.cnblogs.com/leonGuo/archive/2013/03/22/2976068.html

你可能感兴趣的文章
qeephp 记录下
查看>>
The repository 'http://cdn.debian.net/debian stretch Release' is not signed.
查看>>
应用ImageJ对荧光图片进行半定量分析
查看>>
误判心理学常见心理倾向
查看>>
(转)同一服务器部署多个tomcat时的端口号修改详情
查看>>
Git换行符是如何精确控制的
查看>>
c#基础操作
查看>>
【Spring boot】【gradle】idea新建spring boot+gradle项目
查看>>
数据绑定流程分析(校验)
查看>>
这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解)
查看>>
angular 2 中可以注入接口吗?如何实现?
查看>>
针对ArcGIS Server 跨域问题的解释
查看>>
云区域(region),可用区(AZ),跨区域数据复制(Cross-region replication)与灾备(Disaster Recovery)(部分2)...
查看>>
word使用宏定义来统一设置图片大小
查看>>
树莓GPIO &&python
查看>>
Android项目实战(四十四):Zxing二维码切换横屏扫描
查看>>
Android 7.0 行为变更
查看>>
JDK自带方法实现RSA数字签名
查看>>
防止vue组件渲染不更新
查看>>
获取checkbox的选中的值
查看>>