コード例 #1
0
        public ActionResult Index(string year, string month, int?page = 1)
        {
            #region 檢核傳入參數, 並依傳入參數的狀況, 呼叫不同的 Service Method 作查詢

            int intYear  = 0;
            int intMonth = 0;
            IEnumerable <BillingItemViewModel> bills = null;

            //如果 year 與 month 都有值, 那就傳 year / month 的參數作查詢
            //否則, 就查全部
            if (year != null && month != null)
            {
                //如果2者都不是, 那就呼叫服務層作查詢
                if (int.TryParse(year, out intYear) && int.TryParse(month, out intMonth))
                {
                    bills = _billingSvc.GetByQueryYM(intYear, intMonth);
                }
                else
                {
                    bills = new List <BillingItemViewModel>();   //回傳一個空物件
                }
            }
            else
            {
                bills = _billingSvc.GetAll();
            }

            #endregion

            #region 進行分頁

            int pageNumber = (!page.HasValue ? 1 : (page.Value < 1 ? 1 : page.Value));
            var onePage    = bills.ToPagedList(pageNumber, pageSize);

            #endregion

            #region 將結果回傳

            return(View(onePage));

            #endregion
        }