Esempio n. 1
0
        public async Task <IActionResult> CreateAsync(NoticeViewModel noticeVm)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (!ModelState.IsValid)
            {
                return(View(noticeVm));
            }
            var notice = noticeVm.ToModel();

            notice.OwnerId = userId;
            _context.Notices.Add(notice);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", "Home").WithSuccess("", "new notice created"));
        }
Esempio n. 2
0
        public async Task <IActionResult> EditAsync(int id, NoticeViewModel noticeVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(noticeVm));
            }
            var notice = await _context.Notices.AsNoTracking().FirstOrDefaultAsync(n => n.Id == id);

            if (notice == null)
            {
                return(BadRequest());
            }
            notice = noticeVm.ToModel();
            _context.Entry(notice).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index").WithSuccess(string.Empty, $"notice with id {id} edited"));
        }