教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(二)

asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(二)

发布时间:2015-10-10   编辑:jiaochengji.com
asp.net MVC进阶学习--HtmlHelper之GridView控件拓展(二)

    在网上看到的一个不错的asp.net MVC学习系列,希望可以帮助那些正在研究MVC的朋友们。
    本部分内容:asp.net MVC进阶学习--HtmlHelper之GridView控件拓展(二) 。

1.目录结构图
img1

2.自定义集合类

复制代码 代码如下:

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.分页对象

复制代码 代码如下:

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.编辑删除对象
  

复制代码 代码如下:
using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
  
   namespace MvcTest.Code
   {
       public class GridViewOption
       {
          private bool _isEidt;
 
          public bool IsEidt
          {
   get { return _isEidt; }
   set { _isEidt = value; }
          }
 
          private string _editButton;
 
          public string EditButton
          {
   get { return _editButton; }
   set { _editButton = value; }
          }
 
          private bool _isDelete;
 
          public bool IsDelete
          {
   get { return _isDelete; }
   set { _isDelete = value; }
          }
 
          private string _deleteButton;
 
          public string DeleteButton
          {
   get { return _deleteButton; }
   set { _deleteButton = value; }
          }
 
          private string _deleteController;
 
          public string DeleteController
          {
   get { return _deleteController; }
   set { _deleteController = value; }
          }
 
          private string _deleteAction;
 
          public string DeleteAction
          {
   get { return _deleteAction; }
   set { _deleteAction = value; }
          }
 
          private string _editController;
 
          public string EditController
          {
   get { return _editController; }
   set { _editController = value; }
          }
 
          private string _editAction;
 
          public string EditAction
          {
   get { return _editAction; }
   set { _editAction = value; }
          }
 
          private string[] _headers;
 
          public string[] Headers
          {
   get { return _headers; }
   set { _headers = value; }
          }
 
   public GridViewOption(bool isEdit,
   string editButton,
   bool isDelete,
   string deleteButton,
   string[] heander,
   string deleteController,
   string deleteAction,
   string editController,
   string editAction)
          {
   this._isEidt = isEdit;
   this._editButton = editButton;
   this._isDelete = isDelete;
   this._deleteButton = deleteButton;
   this._headers = heander;
   this._deleteController = deleteController;
   this._deleteAction = deleteAction;
   this._editController = editController;
   this._editAction = editAction;
         }
     }
 }

注解:
 _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必须指定,否则编辑或删除的时候就没有指定处理的控制器,就不能处理这些请求。

原文作者:情缘 http://www.cnblogs.com/qingyuan

您可能感兴趣的文章:
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(一)
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(三)
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(二)
asp.net MVC进阶学习---HtmlHelper控件解析(四)
asp.net MVC进阶学习---HtmlHelper控件解析(五)
asp.net MVC进阶学习---HtmlHelper控件解析(二)
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(四)
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(五)
asp.net MVC进阶学习---HtmlHelper控件解析(一)
asp.net MVC进阶学习---HtmlHelper控件解析(三)

[关闭]
~ ~