1.目录结构图
2.自定义集合类
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcTest.Code
{
public class PageList<T> : List<T>
{
private CommonPage _page;
public CommonPage Page
{
get { return _page; }
set { _page = value; }
}
private string _sort;
public string Sort1
{
get { return _sort; }
set { _sort = value; }
}
private string _identityName;
public string IdentityName
{
get { return _identityName; }
set { _identityName = value; }
}
private string _controller;
public string Controller
{
get { return _controller; }
set { _controller = value; }
}
private string _action;
public string Action
{
get { return _action; }
set { _action = value; }
}
public PageList(IEnumerable<T> items,
CommonPage page,
string sort,
string identityName,
string controller,
string action)
{
this.AddRange(items);
this.Page = page;
this._sort = sort;
this._identityName = identityName;
this._controller = controller;
this._action = action;
}
}
} 自定义集合PageList,主要作为扩展的GridView 的数据源,这种扩展的就是为了引入分页对象,排序规则,表格的主键名称,分页所提交的控制器(Controller)和Action。在分页的过程中,Controller和Action处理这个GridView 传递过来的分页请求。定义主键名称则是为了在删除和编辑的时候更好的确定对象。PageList<T>:List<T> 扩展自泛型,就是为了能够能够兼容各种数据模型
3.分页对象
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcTest.Code
{
[Serializable]
public class CommonPage
{
public CommonPage()
{
}
private int _pageSize;
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; }
}
private int _total;
public int Total
{
get { return _total; }
set { _total = value; }
}
private int _pageIndex;
public int PageIndex
{
get { return _pageIndex; }
set { _pageIndex = value; }
}
private int _pageCount;
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; }
}
}
}
这个对象只是为了封装分页的时候一些数据信息:Total 总数据行数, PageSize每页数据行数, PageIndex 页面是第几页, PageCount 总页数. 这个类其实是为PageList<T> 这个类服务的,我们也可以将这些属性封装到PageList<T>当中去,只不过PageList<T> 中的参数过多,显示有些不美观
4.编辑删除对象对象
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace MvcTest.Code
7 {
8 public class GridViewOption
9 {
10 private bool _isEidt;
11
12 public bool IsEidt
13 {
14 get { return _isEidt; }
15 set { _isEidt = value; }
16 }
17
18 private string _editButton;
19
20 public string EditButton
21 {
22 get { return _editButton; }
23 set { _editButton = value; }
24 }
25
26 private bool _isDelete;
27
28 public bool IsDelete
29 {
30 get { return _isDelete; }
31 set { _isDelete = value; }
32 }
33
34 private string _deleteButton;
35
36 public string DeleteButton
37 {
38 get { return _deleteButton; }
39 set { _deleteButton = value; }
40 }
41
42 private string _deleteController;
43
44 public string DeleteController
45 {
46 get { return _deleteController; }
47 set { _deleteController = value; }
48 }
49
50 private string _deleteAction;
51
52 public string DeleteAction
53 {
54 get { return _deleteAction; }
55 set { _deleteAction = value; }
56 }
57
58 private string _editController;
59
60 public string EditController
61 {
62 get { return _editController; }
63 set { _editController = value; }
64 }
65
66 private string _editAction;
67
68 public string EditAction
69 {
70 get { return _editAction; }
71 set { _editAction = value; }
72 }
73
74 private string[] _headers;
75
76 public string[] Headers
77 {
78 get { return _headers; }
79 set { _headers = value; }
80 }
81
82
83 public GridViewOption(bool isEdit,
84 string editButton,
85 bool isDelete,
86 string deleteButton,
87 string[] heander,
88 string deleteController,
89 string deleteAction,
90 string editController,
91 string editAction)
92 {
93 this._isEidt = isEdit;
94 this._editButton = editButton;
95 this._isDelete = isDelete;
96 this._deleteButton = deleteButton;
97 this._headers = heander;
98 this._deleteController = deleteController;
99 this._deleteAction = deleteAction;
100 this._editController = editController;
101 this._editAction = editAction;
102 }
103 }
104 }
105 _isEidt = isEdit; 表示是否显示编辑
_editButton = editButton; 编辑按钮显示字样
_isDelete = isDelete; 表示是否显示删除
_deleteButton = deleteButton; 删除按钮显示字样
_headers = heander; 这个是数组类型,表示显示标题的字样
_deleteController = deleteController; 删除的时候由哪个控制器处理
_deleteAction = deleteAction; 删除的时候由哪个控制器中的action处理
_editController = editController; 编辑的时候由哪个控制器处理
_editAction = editAction; 编辑的时候由哪个控制器中的action处理
如果IsEdit,IsDelete的值设置为false 的话,即使对应的编辑按钮设置了值也不能显示,以后的Controller 和Action必须指定,否则编辑或删除的时候就没有指定处理的控制器,就不能处理这些请求