public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var emptyBug = new Bug();

            if (await TryUpdateModelAsync <Bug>(
                    emptyBug, "bug", x => x.Title, x => x.Severity))
            {
                emptyBug.CreateDate = DateTime.Now;

                _context.Bug.Add(emptyBug);
                await _context.SaveChangesAsync();

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


            _context.Bug.Add(Bug);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var bug = await _context.Bug.AsNoTracking().FirstOrDefaultAsync(x => x.BugID == id);

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

            try
            {
                _context.Bug.Remove(bug);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException)
            {
                return(RedirectToAction("./Delete", new { id, saveChangesError = true }));
            }
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var bugtoUpdate = await _context.Bug.FindAsync(id);

            if (await TryUpdateModelAsync <Bug>(
                    bugtoUpdate,
                    "bug",
                    s => s.Title, s => s.Severity, s => s.CreateDate))
            {
                await _context.SaveChangesAsync();

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

            return(Page());


            //Orginal Update Code before using FindAsync and TryUpdateModel
            #region Method Original
            //if (!ModelState.IsValid)
            //{
            //    return Page();
            //}

            //_context.Attach(Bug).State = EntityState.Modified;

            //try
            //{
            //    await _context.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!BugExists(Bug.BugID))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return RedirectToPage("./Index");
            #endregion
        }
Exemple #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                Message = "";
                return(Page());
            }

            _context.Customers.Add(Customer);
            await _context.SaveChangesAsync();

            Message = "Customer Added.";
            return(RedirectToPage("./Index"));
            //return Page();
        }