Exemplo n.º 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
        }
Exemplo n.º 2
0
        public ActionResult CreateWithAjax(BillingItemViewModel item, int?page)
        {
            //定位在當初輸入資料的那個值
            //不論 ModelState 是否為 Valid, 都要執行, 不然萬一 Model 驗證失敗, 就沒有 SelectList 可以用, 會造成例外 ...
            ViewBag.BillTypes = new SelectList(GlobalCodeMappings.BillTypes, "Key", "Value", item.BillType);
            Thread.Sleep(3 * 1000); //暫停一下, 看效果

            #region Model 檢查

            if (!ModelState.IsValid)
            {
                //參考 https://gist.github.com/jpoehls/2230255 取得錯誤的作法
                var errors = ModelState
                             .Where(x => x.Value.Errors.Count > 0)
                             .Select(d => new AjaxErrorViewModel
                {
                    ClientId     = d.Key,
                    ErrorMessage = d.Value.Errors.FirstOrDefault().ErrorMessage
                });
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(errors));

                #region 參考 91 老師的實作方式
                //var errorFields = ModelState.Where(d => d.Value.Errors.Any())
                //     .Select(x => new { x.Key, x.Value.Errors });
                //var errors = new List<AjaxErrorViewModel>();
                //foreach (var err in errorFields)
                //{
                //    errors.Add(err.Errors.Select(
                //        d => new AjaxErrorViewModel()
                //        {
                //            ClientId = err.Key,
                //            ErrorMessage = d.ErrorMessage
                //        }).FirstOrDefault());
                //}
                //Response.StatusCode = (int)HttpStatusCode.BadRequest;
                //return Json(errors);
                #endregion

                #region 最原始的作法
                ////不知應該如何處理錯誤訊息 (in AJAX Helper) ...
                ////有試過 return View("Create", item); 但發現畫面會亂掉
                ////
                //List<string> errors = new List<string>();
                //foreach (ModelState err in ModelState.Values)
                //{
                //    foreach (ModelError errmsg in err.Errors)
                //    {
                //        errors.Add(errmsg.ErrorMessage);
                //    }
                //}

                ////以下參考 ...
                ////http://stackoverflow.com/questions/18763993/send-exception-message-in-ajax-beginform-mvc-4-scenario
                //Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;    //強迫前端執行 OnFailure ...
                //return Json(errors);
                #endregion
            }

            #endregion

            #region 呼叫服務層

            //呼叫 Service 層提供的功能
            item.Id = Guid.NewGuid();
            _billingSvc.Add(item);

            #endregion

            #region 分頁處理

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

            #endregion

            #region 回傳結果

            return(PartialView("ListCurrent", onePage));

            //return PartialView("ListCurrent", bills);

            #endregion
        }
Exemplo n.º 3
0
        public ActionResult Index()
        {
            var data = billingService.GetAll();

            return(View(data));
        }