public async Task <ActionResult <NoticeDto> > CreateNotice([FromBody] NoticeAddDto noticeAddDto) { //ApiController在遇到noticeAddDto为空时可以自动返回400错误 var notice = _mapper.Map <Notice>(noticeAddDto); _noticeRepository.AddNotice(notice);//只是被添加到DbContext里 await _noticeRepository.SaveAsync(); var noticeDto = _mapper.Map <NoticeDto>(notice); return(CreatedAtRoute(nameof(GetNotice), new { noticeId = notice.NoticeId }, noticeDto)); }
public IActionResult Index(Notice notice) { if (ModelState.IsValid) { _noticeRepository.AddNotice(notice); return(RedirectToAction("AddNoticeComplete")); } return(View()); }
public IActionResult CreateNotice([FromBody] Notice notice) { if (notice == null) { return(BadRequest()); } if (string.IsNullOrEmpty(notice.Description)) { ModelState.AddModelError("Description", "The description should not be empty! "); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var createdNote = _noticeRepository.AddNotice(notice); return(Created("notice", createdNote)); }
public IActionResult Post(L_Notice notice) { _noticeRepository.AddNotice(notice); return(CreatedAtAction(nameof(GetById), new { id = notice.NoticeId }, notice)); }