コード例 #1
0
ファイル: Edit.cshtml.cs プロジェクト: winni21kog/CRUDExample
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Razor Page Scaffolding
            //_context.Attach(DailyRecord).State = EntityState.Modified;

            // 由資料庫取得修改前資料
            var origin = _context.Records.SingleOrDefault(o => o.Id == DailyRecord.Id);

            if (origin == null)
            {
                return(NotFound());
            }

            // 檢查日期是否被更動,若是則拒絕更新 早於:<0 等於=0 晚於>0
            if (origin.Date.CompareTo(DailyRecord.Date) != 0)
            {
                ModelState.AddModelError("DailyRecord.Date", "日期不可修改");
                return(Page());
            }

            // 正向表列方式更新欄位,只更新有異動的部分
            origin.Status       = DailyRecord.Status;
            origin.EventSummary = DailyRecord.EventSummary;
            origin.Remark       = DailyRecord.Remark;
            origin.User         = DailyRecord.User;

            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
コード例 #2
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //由資料庫取得修改前版本
            var orig = _context.Records.SingleOrDefault(o => o.Id == DailyRecord.Id);

            if (orig == null)
            {
                return(NotFound());
            }

            //檢查日期是否被更動,若是,拒絕更新
            if (orig.Date.CompareTo(DailyRecord.Date) != 0)
            {
                ModelState.AddModelError("DailyRecord.Date", "日期不可修改");
                return(Page());
            }

            //以正向表列方式更新可更新欄位,SaveChanges()只會更新有異動的欄位
            orig.Status       = DailyRecord.Status;
            orig.Remark       = DailyRecord.Remark;
            orig.EventSummary = DailyRecord.EventSummary;
            orig.User         = DailyRecord.User;

            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
コード例 #3
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(DailyRecord).State = EntityState.Modified;

            //檢查日期是否被更動,若是,拒絕更新
            if (_context.Entry(DailyRecord).Property(o => o.Date).IsModified)
            {
                ModelState.AddModelError("DailyRecord.Date", "日期不可修改");
                return(Page());
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DailyRecordExists(DailyRecord.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DailyRecord = await _context.Records.FindAsync(id);

            if (DailyRecord != null)
            {
                _context.Records.Remove(DailyRecord);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
コード例 #5
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // 檢查輸入資料是否重複,若是顯示錯誤訊息
            if (_context.Records.Any(x => x.Date == DailyRecord.Date))
            {
                ModelState.AddModelError("DailyRecord.Date", "該日期紀錄已存在");
                return(Page());
            }
            _context.Records.Add(DailyRecord);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }