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

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

时间: 2024-09-05 17:00:59

MVC进阶学习--HtmlHelper之GridView控件拓展(二)的相关文章

MVC进阶学习--HtmlHelper之GridView控件拓展(五)

1.GridView使用Action代码   Code 1 public ActionResult Index() 2         { 3             CommonPage page = TempData["page"] as CommonPage; 4             if (page == null) 5             { 6                 page = new CommonPage(); 7             } 8   

MVC进阶学习--HtmlHelper之GridView控件拓展(一)

      最近用MVC做项目的时候,感觉脱离了原有WebForm的那种编程方式,心中略有想法.在WebForm中由一个很常用的数据绑定控件GridView,我相信用过.net的同仁都会使用这个控件,在开发中的确给我们带来了不少的方便.而现在的MVC改变了原有的那种模式,没有了控件编程,输出表格都用foreach,for 循环之类的,似乎有些麻烦了.于是自己写了一个扩展标签GridView.写完之后感觉还可以,给各位分享一下.       1.功能介绍             (1).该扩展标签

MVC进阶学习--HtmlHelper之GridView控件拓展(三)

1.扩展核心代码 Code  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Web.Mvc.Html;  6 using System.Web.Mvc;  7 using MvcTest.Code;  8 using System.Web.UI;  9 using System.IO; 10 using System.D

MVC进阶学习--HtmlHelper控件解析(四)

1.RenderPartialExtensions类      RenderPartialExtensions类主要扩展了一个方法 RenderPartial()      RenderPartial(string partialViewName);      RenderPartial(string partialViewName,ViewDataDictionary viewData);      RenderPartial(string partialViewName,object mod

MVC进阶学习--HtmlHelper控件解析(二)

1.InputExtensions类      InputExtensions类主要有5种类型的扩展方法,分别用于CheckBox控件,Hidden控件,Pass控件,RadionButton控件,TextBox控件 2.CheckBox控件      有如下重载方法:      CheckBox(string name);      CheckBox(string name,bool isChecked);      CheckBox(string name,book isChecked,ob

MVC进阶学习--HtmlHelper控件解析(五)

1.SelectExtensions 类      SelectExtensions 主要扩展了两种类型的方法 DropDowList和ListBox,这两个方法主要区别是后者添加了一个属性multiple="multiple",设置这个属性主要是为了能够多选 2.DropDowList使用例子代码       Code<tr>            <td width="100" align="right">      

学习使用Material Design控件(二)使用DrawerLayout实现侧滑菜单栏效果

本文介绍如何使用DrawerLayout和NavigationView实现侧滑菜单栏的效果. 效果如下: Layout布局 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+

MVC进阶学习--HtmlHelper控件解析(一)

1.HtmlHelper类      HtmlHelper类位于System.Web.MVC.Html命名空间下.主要包括FormExtensions,InputExtensions,LinkExtensions,SelectExtensions,TextAreaExtensions,ValidationExtensions,RenderPartialExtensions等7个静态内,他们全部是是采用拓展方法来实现的      在asp.net MVC中ViewPage中使用的属性如下:    

MVC进阶学习--HtmlHelper控件解析(三)

1.LinkExtensions类      该类主要用于生成相关链接,主要扩展了ActionLink和RouteLink方法 2.ActionLink       ActionLink扩展方法主要实现一个连接,共有十个重载方法      ActionLink(string linkText,string actionName);      ActionLink(string linkText,string actionName,object routeValues);      ActionL