Html辅助方法(分页、下拉框)

引用命名空间:

using System.Text;
using System.Web.Mvc;

Html分页方法

#region 分页Html辅助方法
        /// <summary>
        /// 分页Html辅助方法
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="parameterString"></param>
        /// <returns></returns>
        public static HtmlString PageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount, string parameterString)
        {
            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1);//总页数
            var output = new StringBuilder();
            output.Append("<nav>");
            output.Append("<ul class='pagination'>");
            string pageSizrWithParameter = string.Empty;
            if (!string.IsNullOrEmpty(parameterString))
                pageSizrWithParameter = pageSize + "&" + parameterString;
            if (totalPages>1)
            {
                output.AppendFormat("<li><a href='{0}?pageIndex=1&pageSize={1}' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>",redirectTo,pageSizrWithParameter);
                if (currentPage > 1)//处理上一页连接
                    output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>上一页</a></li>",redirectTo,currentPage-1,pageSizrWithParameter);

                output.Append("");
                int currint = 5;
                for (int i = 0; i < 10; i++)
                {//一共最多显示10个页码,前面五个后面五个
                    if ((currentPage+i-currint)>=1 && (currentPage+1-currint)<=totalPages)
                    {
                        if (currint == i)//当前页处理
                            output.AppendFormat("<li class='active'><a href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li>", redirectTo, currentPage, pageSizrWithParameter, currentPage);
                        else//一般页处理
                            output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>{3}</a></li>",redirectTo,currentPage+i-currint,pageSizrWithParameter,currentPage+i-currint);
                    }
                    output.Append("");
                }
                if (currentPage < totalPages)//处理下一页连接
                    output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'>下一页</a></li>", redirectTo, currentPage + 1, pageSizrWithParameter);
                output.Append("");

                if (currentPage != totalPages)
                    output.AppendFormat("<li><a href='{0}?pageIndex={1}&pageSize={2}'><span aria-hidden='true'>&raquo;</span></a></li>", redirectTo, totalPages, pageSizrWithParameter);
                output.Append("");
            }
            output.Append("</ul>");
            output.Append("</nav>");

            return new HtmlString(output.ToString());
        }
        #endregion
View Code

控制器方法(搜索的关键字在Js中拼接出来,然后用window.location="路径?参数="+。。+"&参数="+。。。+"。。。。")

[HttpGet]
public ActionResult Moments(int pageIndex=1,int pageSize=20)
{
     int totalRecord=0;
     List<实体类> list=得到集合方法(pageIndex,pageSize,out totalRecord);
     ViewData["totalRecord"]=totalRecord;
     ViewData["pageIndex"]=pageIndex;
     ViewData["pageSize"]=pageSize;

     #region 生成搜索状态保存数据
     StringBuilder sb=new StringBuilder();
     foreach(string item in Request.QueryString.AllKeys)
     {
          if(!item.Equals("pageIndex") && !item.Equals("pageSize"))
                 sb.Append(item+"="+Request.QueryString[item]+"&")  
     } 
     ViewData["parameter"]=sb.ToString().Trim('&');
     #endregion
     return View(lam);
}
View Code

引用分页

<!--在控制器里面存储的ViewData,totalRecord表示根据添加查询到的数据并返回的条数,parameter表示搜索条件(关键字搜索等等)-->
@Html.PageNavigate(int.Parse(ViewData["pageIndex"].ToString()),int.Parse(ViewData["pageSize"].ToString()),int.Parse(ViewData["totalRecord"].ToString()),ViewData["parameter"].ToString())

DropDownList:(还没有经过测试,只是展示一下思路,我也不清楚理解是否是正确的,求大神们指教一下)

public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> list)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name!=null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list!=null)
            {
                foreach (var item in list)
                {
                    option.MergeAttribute("value", item.Value);
                    option.InnerHtml = item.Text;
                    select.InnerHtml += option;
                }
            }
            return new HtmlString(select.ToString());
            //return ExtDropDownList(htmlHelper, select.ToString(), null);
        }
View Code
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> list, IDictionary<string,object> htmlAttribute)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    option.MergeAttribute("value", item.Value);
                    option.InnerHtml = item.Text;
                    select.InnerHtml += option;
                }
            }
            select.MergeAttributes(htmlAttribute);
            return new HtmlString(select.ToString());
        }
View Code
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    option.MergeAttribute("value", item.Value);
                    option.InnerHtml = item.Text;
                    select.InnerHtml += option;
                }
            }
            return ExtDropDownList(htmlHelper, select.ToString(), null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute));
        }
View Code
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        if (item.Value == value)
                            option.MergeAttribute("value", value);
                        
                    }
                    else
                        option.MergeAttribute("value", item.Value);

                    option.InnerHtml = item.Text;

                    select.InnerHtml += option;
                }
            }
            return new HtmlString(select.ToString());
        }
View Code
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, IDictionary<string, object> htmlAttribute)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        if (item.Value == value)
                            option.MergeAttribute("value", value);

                    }
                    else
                        option.MergeAttribute("value", item.Value);

                    option.InnerHtml = item.Text;

                    select.InnerHtml += option;
                }
            }
            select.MergeAttributes(htmlAttribute);
            return new HtmlString(select.ToString());
        }
View Code
public static HtmlHelper ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute, IDictionary<string, object> htmlAttribute)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        if (item.Value == value)
                            option.MergeAttribute("value", value);

                    }
                    else
                        option.MergeAttribute("value", item.Value);

                    option.InnerHtml = item.Text;

                    select.InnerHtml += option;
                }
            }
            select.MergeAttributes(htmlAttribute);
            return ExtDropDownList(htmlHelper, select.ToString(), null, null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute), null);
        }
View Code
public static HtmlString ExtDropDownList(this HtmlHelper htmlHelper, string name, string value, IEnumerable<SelectListItem> list, object attribute1, object attribute2)
        {
            TagBuilder select = new TagBuilder("select");
            TagBuilder option = new TagBuilder("option");
            if (!select.Attributes.ContainsKey("id") && name != null)
                select.GenerateId(name);//添加Id属性

            if (!String.IsNullOrEmpty(name))
                select.MergeAttribute("name", name);//添加那么属性

            //添加节点
            option.MergeAttribute("value", "0");
            option.InnerHtml = "--请选择--";
            select.InnerHtml += option;
            if (list != null)
            {
                foreach (var item in list)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        if (item.Value == value)
                            option.MergeAttribute("value", value);

                    }
                    else
                        option.MergeAttribute("value", item.Value);

                    option.InnerHtml = item.Text;

                    select.InnerHtml += option;
                }
            }
            return ExtDropDownList(htmlHelper, select.ToString(), null, null, HtmlHelper.AnonymousObjectToHtmlAttributes(attribute1), HtmlHelper.AnonymousObjectToHtmlAttributes(attribute2));
        }
View Code

 

posted @ 2015-10-02 10:54  如此低调的男人  阅读(2056)  评论(0编辑  收藏  举报