2012年10月20日土曜日

[ASP.NET MVC] WebGrid のページング(2)

参考サイト
http://msdn.microsoft.com/ja-jp/magazine/hh288075.aspx

サーバー側のページ切り替えをやってみるテスト。
表示に必要なデータだけをDBから取得する。そしてWebGridでページングする。

■サーバー側のデータ取得(コントローラー)

        /// <summary>
        /// ページングのテスト
        /// </summary>
        /// <returns></returns>
        public ActionResult Paging(int? page)
        {

            // page は WebGrid が生成するページングリンクのURLに
            // GETパラメータとして指定されている
            return View( new PaginatedList<SchoolViewModel>( 

                              getData() , (page != null ? (int)page - 1: 0) , 3 )
            );
        }

        /// <summary>
        /// データを取得します
        /// </summary>
        /// <returns></returns>
        private IQueryable<SchoolViewModel> getData()
        {
            Models.hogeEntities db = new Models.hogeEntities();
            return
                (from sc in db.SchoolClass
                 join s in db.Schooler
                 on sc.ClassID equals s.ClassID
                 orderby sc.Grade, sc.Class
                 select new SchoolViewModel()
                 {
                     Grade = sc.Grade
                    ,
                     Class = sc.Class
                    ,
                     name = s.Name
                 });
        }



ここ↓のPaginatedListというクラスをそのまま流用している
http://www.atmarkit.co.jp/fdotnet/scottgublog/nerddinner/part08.html

■PaginatedList

    public class PaginatedList<T> : List<T>
    {

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }



コンストラクタでIQueryableのデータをSkip&Takeして、PageIndexに該当するデータを取得してる。



■chtml

@model MvcApplication1.Models.PaginatedList<MvcApplication1.Models.SchoolViewModel>

@{
    ViewBag.Title = "Paging";
    Layout = "~/Views/Shared/_Layout.cshtml";

    // WebGridオブジェクト生成
    var grid = new WebGrid(null, rowsPerPage: Model.PageSize);
    grid.Bind(Model, rowCount: Model.TotalCount, autoSortAndPage: false);
}

<h2>Paging</h2>

@* WebGridオブジェクトでレンダリング *@
@grid.GetHtml(columns:grid.Columns(
grid.Column("Class","クラス", canSort:false)
    ,grid.Column("Grade", "学年", canSort:false)
    ,grid.Column("Name", "名前", canSort:false)
    )
)


ポイントっぽいところ
・WebGridのコンストラクタで source にnullを指定する
・Bindメソッドでページの表示に必要なデータと全体の件数を渡し、autoSortAndPage を false にする
(autoSortAndPage を false にするとデフォルトのソート機能も無効になる)






WebGridにページに表示する分のデータしか渡してないけど
ちゃんとページングのリンクを生成してくれている。

3 件のコメント:

  1. Ideas! i think no need of any casino jobs
    ideas just get to gather and start having fun, chit chat and other activities.http://pennykent.simplesite.com/

    返信削除
  2. 4 extra slot games ready to spin. Of course, for the online casino casino royale hbo evaluation site naturally you will not miss getting a additional slot game. Are you ready to introduce it? From the gone to the present, every players in imitation of to attempt extra ways of spinning in online slot games. That's why the main other game camps tend to proclaim game ads 2 to 3 weeks previously they actually play, subsequent to the mean of letting players see the game preview first. Attract and advertise these are the 4 additional slot games to be released this year.Wizard Slot the first game launched today is a 5-reel, 178 payline video slot called The Whiz Slot, created by ELK Studio, hoping players will allow risks in the adventure world. Mysticism, learning every kinds of spells. Not deserted that, all times you start to spin you will have the opportunity to combine special items, get powder and make wizards including getting items into this magical world.

    返信削除
  3. Today, playing online casinos is very popular. You can pick to put on an act wealthily anytime, anywhere, anytime, anywhere, you can easily choose; you can pick to participate in the game 24 hours a day; like the back of highly developed technology, betting can be made more easily Because it works upon both phones and computers but many people want to know what's stand-in amongst the two. perform online casino via mobile phone. Is a advanced form of gambling where you can choose to bet easily anywhere, whether in the bathroom, bedroom, or car, even at good distances. being clever to access especially those without a computer can easily bet. Many people who choose to gamble online tend to have a mobile phone, whether it's Android or iOS, to participate in the bet. Many would arrive in affable for gambling at an online casino via mobile phone. The screen format is the same. However, it may not be within acceptable limits casino royale cast name for anyone to enactment the game from the beginning, because sometimes it may not be competent to look the details clearly. You must pay attention to this. realize nt look cool. accomplish nt bother roughly your talent similar to choosing to take action the game.

    返信削除