• asp.net mvc使用的心得和问题的解决方法(陆续更新ing)


    1、在mvc下webform的分页控件不能用,只好自己山寨一个轻便的。

      1    public class PageHelp
      2    {
      3        /// <summary>
      4        /// 总页数
      5        /// </summary>

      6        public int TotalPageNum getset; }
      7        /// <summary>
      8        /// 当前页
      9        /// </summary>

     10        public int CurrentPageNum getset; }
     11
     12        private int pageNumSize = 5;
     13        /// <summary>
     14        /// 页码链接允许的数量,默认为5个链接
     15        /// </summary>

     16        public int PageNumSize
     17        {
     18            get return pageNumSize; }
     19            set { pageNumSize = value; }
     20        }

     21        /// <summary>
     22        /// 是否允许向上翻页
     23        /// </summary>

     24        public bool canUp
     25        {
     26            get
     27            {
     28                if (CurrentPageNum == 1)
     29                    return false;
     30                else
     31                    return true;
     32            }

     33        }

     34        /// <summary>
     35        /// 是否允许向下翻页
     36        /// </summary>

     37        public bool canDown
     38        {
     39            get
     40            {
     41                if (CurrentPageNum == TotalPageNum)
     42                    return false;
     43                else
     44                    return true;
     45            }

     46        }

     47        /// <summary>
     48        /// 页码数组
     49        /// </summary>

     50        public int[] PageNumArray
     51        {
     52            get
     53            {
     54                if (TotalPageNum <= pageNumSize)
     55                {
     56                    int[] pageArray = new int[TotalPageNum];
     57                    for (int i = 0; i < TotalPageNum; i++)
     58                        pageArray[i] = i + 1;
     59                    return pageArray;
     60                }

     61                else
     62                {
     63                    int[] pageArray = new int[pageNumSize];
     64                    if (CurrentPageNum == 1 || TotalPageNum <= pageNumSize)
     65                    {
     66                        for (int i = 0; i < pageNumSize; i++)
     67                            pageArray[i] = i + 1;
     68                    }

     69                    else if (CurrentPageNum == TotalPageNum)
     70                    {
     71                        for (int i = 0; i < pageNumSize; i++)
     72                            pageArray[i] = TotalPageNum - pageNumSize + i + 1;
     73                    }

     74                    else
     75                    {
     76                        int numHalf = pageNumSize / 2;
     77                        if (CurrentPageNum - numHalf <= 0)
     78                        {
     79                            for (int i = 0; i < pageNumSize; i++)
     80                                pageArray[i] = i + 1;
     81                        }

     82                        else if (CurrentPageNum + (pageNumSize - numHalf) >= TotalPageNum)
     83                        {
     84                            for (int i = 0; i < pageNumSize; i++)
     85                                pageArray[i] = TotalPageNum - (pageNumSize - i - 1);
     86                        }

     87                        else
     88                        {
     89                            int i;
     90                            for (i = 0; i < numHalf; i++)
     91                            {
     92                                pageArray[i] = CurrentPageNum - numHalf + i;
     93                            }

     94                            for (int z = 1; z <= pageNumSize - numHalf; z++)
     95                            {
     96                                pageArray[i + z - 1= CurrentPageNum + z - 1;
     97                            }

     98                        }

     99                    }

    100                    return pageArray;
    101                }

    102            }

    103        }

    104        public PageHelp(int totalPage, int currentPage, int pageNumSize)
    105        {
    106            TotalPageNum = totalPage;
    107            CurrentPageNum = currentPage;
    108            PageNumSize = pageNumSize;
    109        }

    110        public string PagePrint(string href)
    111        {
    112            string tableHtml = string.Empty;
    113            string replace = "$page";
    114            tableHtml += "<table cellpadding='0' cellspacing='0' class='" + "pagination " + "'><tr>";
    115            tableHtml += "<td ><div class='pageinfo'> 共<span>" + TotalPageNum + "</span>" + "页<div></td>";
    116            if (CurrentPageNum != 1)
    117            {
    118                tableHtml += "<td><a href='" + href.Replace("$page","1"+ "'>第一页</a></td>";
    119                tableHtml += "<td><a href='" + (href.Replace(replace,(CurrentPageNum - 1).ToString())) + "'>上一页</a></td>";
    120            }

    121            else
    122            {
    123                tableHtml += "<td><span class='cannot'>第一页</span></td>";
    124                tableHtml += "<td><span class='cannot'>上一页</span></td>";
    125            }

    126            for (int i = 0; i < PageNumArray.Count(); i++)
    127            {
    128                if (PageNumArray[i] != CurrentPageNum)
    129                {
    130                    tableHtml += "<td>";
    131                    tableHtml += "<a href='";
    132                }

    133                else
    134                {
    135                    tableHtml += "<td>";
    136                    tableHtml += "<a class='current' href='";
    137                }

    138                tableHtml += href.Replace(replace,PageNumArray[i].ToString());
    139                tableHtml += "'>";
    140                tableHtml += PageNumArray[i];
    141                tableHtml += "</a>";
    142                tableHtml += "</td>";
    143            }

    144            if (canDown)
    145            {
    146                tableHtml += "<td><a href='" + (href.Replace(replace,(CurrentPageNum+1).ToString())) + "'>下一页</a></td>";
    147                tableHtml += "<td><a href='" + href.Replace(replace,TotalPageNum.ToString())+ "'" + ">最后一页</a></td>";
    148            }

    149            else
    150            {
    151                tableHtml += "<td><span class='cannot'>下一页</span></td>";
    152                tableHtml += "<td><span class='cannot'>最后一页</span></td>";
    153            }

    154            tableHtml += "</tr></table>";
    155            return tableHtml;
    156        }

    157    }
    分页样式

    使用示例

    使用示例

    效果:

    2、[HandleError]失灵了,囧

    这个问题很好解决。。。

    <customErrors mode="On" />只要在web.config里加上这句就可以了,汗啊。。。

    在Error页面的使用示例:

    使用示例

    3、不标明bind字段就stackoverflow的

    asp.net mvc的ModelBinder很好很强大,可是如果你要构建的实体类的字段没有全部post过来,而且你也没用bind(...)显示标明要bind的属性的时候,呵呵,stackoverflow这个异常就会华丽的抛出。。。,为什么不能智能点,找不到属性你还找。。。,结果就溢出了,看来还有改进的余地。。。

  • 相关阅读:
    嵌套类型返回错误解决办法(如迭代器的设计)
    UITableView的多选删除模式
    UITableView的编辑模式
    指针和引用初理解
    strstr()函数实现
    一句话的单词倒置
    字符串过滤程
    strcpy函数
    二叉排序树(Binary Sort Tree)
    二叉树插入操作
  • 原文地址:https://www.cnblogs.com/brightwang/p/1336370.html
Copyright © 2020-2023  润新知