示例#1
0
        public ActionResult Edit(BillingItemViewModel item)
        {
            //定位在當初輸入資料的那個值
            ViewBag.BillTypes = new SelectList(GlobalCodeMappings.BillTypes, "Key", "Value", item.BillType);

            #region Model 檢查

            if (!ModelState.IsValid)
            {
                return(View(item));
            }

            #endregion

            #region 呼叫服務層

            try
            {
                _billingSvc.Edit(item);
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View(item));
            }

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

            #region Model 檢查

            if (!ModelState.IsValid)
            {
                return(View(item));
            }

            #endregion

            #region 呼叫服務層

            item.Id = Guid.NewGuid();
            _billingSvc.Add(item);

            #endregion

            #region 回傳結果

            return(View());

            #endregion
        }
示例#3
0
        public ActionResult Delete(Guid?id)
        {
            #region 檢查參數

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            #endregion

            #region 確認欲刪除的資料是否存在

            //這裡必須用 id.Value, 不然編譯會出錯
            BillingItemViewModel item = _billingSvc.GetSingle(id.Value);
            if (item == null)
            {
                return(HttpNotFound());
            }

            #endregion

            #region 其它處理

            item.BillTypeName = GlobalCodeMappings.BillTypes[item.BillType];

            #endregion

            #region 回傳結果

            return(View(item));

            #endregion
        }
示例#4
0
        public ActionResult Edit(Guid?id)
        {
            #region 檢查參數

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            #endregion

            #region 確認欲編輯的資料是否存在

            //這裡必須用 id.Value, 不然編譯會出錯
            BillingItemViewModel item = _billingSvc.GetSingle(id.Value);
            if (item == null)
            {
                return(HttpNotFound());
            }

            #endregion

            #region 其它處理

            //定位在當初輸入資料的那個值
            ViewBag.BillTypes = new SelectList(GlobalCodeMappings.BillTypes, "Key", "Value", item.BillType);

            #endregion

            #region 回傳結果

            return(View(item));

            #endregion
        }
示例#5
0
        public void Delete(BillingItemViewModel item)
        {
            //原理: 由資料庫再取一次舊資料, 確認資料存在後, 才作刪除
            var oldItem = _billingRep.GetSingle(x => x.Id == item.Id);

            if (null == oldItem)
            {
                throw new Exception("您欲刪除的資料不存在, 請檢查是否已被刪除.");
            }
            _billingRep.Remove(oldItem);
        }
示例#6
0
        /// <summary>
        /// 新增資料
        /// </summary>
        /// <param name="item"></param>
        public void Add(BillingItemViewModel item)
        {
            //不可在這裡作 using, 萬一新增完後, 要作查詢, 那個 uow 就不見了
            //using (uow)
            //{
            //    _billingBo.Add(item);
            //    uow.Commit();
            //}

            _billingBo.Add(item);
            uow.Commit();
        }
示例#7
0
        public PartialViewResult AddItem(int index)
        {
            var model = new BillingItemViewModel {
                Index = index
            };

            ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("BillingItems[{0}]", index);

            model.Items = _billingContext.GetActiveItems().Select(Map).ToList();

            return(PartialView("BillingItemViewModel", model));
        }
示例#8
0
        /// <summary>
        /// 新增資料
        /// </summary>
        /// <param name="item"></param>
        public void Add(BillingItemViewModel item)
        {
            var acc = new AccountBook
            {
                Id         = item.Id,
                Categoryyy = item.BillType,
                Dateee     = item.BillDate,
                Amounttt   = item.Amount,
                Remarkkk   = item.Memo
            };

            _billingRep.Create(acc);
        }
示例#9
0
        public void Edit(BillingItemViewModel item)
        {
            //註: 因為是網頁線上程式, 不是批次, 最好確認一下前端輸入的資料, 確定資料是否存在, 才作刪除.
            //    以本Homework 的功能而言, 效能不是最大的考量因素, 安全性比較需要考慮
            //原理: 由資料庫再取一次舊資料, 確認資料存在後, 才作修改
            var oldItem = _billingRep.GetSingle(x => x.Id == item.Id);

            if (null == oldItem)
            {
                throw new Exception("您欲修改的資料不存在, 請檢查是否已被刪除.");
            }
            oldItem.Categoryyy = item.BillType;
            oldItem.Dateee     = item.BillDate;
            oldItem.Amounttt   = item.Amount;
            oldItem.Remarkkk   = item.Memo;
        }
示例#10
0
        public ActionResult Create(int?page)
        {
            #region 建立初始資料

            var obj = new BillingItemViewModel()
            {
                BillDate = DateTime.Today
            };
            ViewBag.BillTypes = new SelectList(GlobalCodeMappings.BillTypes, "Key", "Value", -1);

            #endregion

            #region 回傳結果

            return(View(obj));

            #endregion
        }
示例#11
0
        /// <summary>
        /// 以 Id 作查詢, 取回一筆資料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillingItemViewModel GetSingle(Guid id)
        {
            var acc = _billingRep.GetSingle(x => x.Id == id);
            BillingItemViewModel bill = null;

            if (null != acc)
            {
                //將 Model 轉換為 ViewModel
                bill = new BillingItemViewModel
                {
                    Id       = acc.Id,
                    BillType = acc.Categoryyy,
                    BillDate = acc.Dateee,
                    Amount   = acc.Amounttt,
                    Memo     = acc.Remarkkk
                };
            }
            return(bill);
        }
示例#12
0
 public void Delete(BillingItemViewModel item)
 {
     _billingBo.Delete(item);
     uow.Commit();
 }
示例#13
0
 public void Edit(BillingItemViewModel item)
 {
     _billingBo.Edit(item);
     uow.Commit();
 }
示例#14
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
        }