public async Task <IActionResult> CreateOrUpdate(int?id) { var model = new CreateOrUpdateAuthorViewModel(); if (id != null) { var data = await _context.Authors.FindAsync(id); model = _mapper.Map <CreateOrUpdateAuthorViewModel>(data); if (model == null) { return(NotFound()); } } return(View(model)); }
public async Task <IActionResult> Create(CreateOrUpdateAuthorViewModel Author) { if (ModelState.IsValid) { Author.Alias = Author.Name.ToFriendlyUrl(); Author.UpdateDate = DateTime.Now; var viewModel = _mapper.Map <Author>(Author); _context.Add(viewModel); Alert("Lưu danh mục thành công!"); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(nameof(CreateOrUpdate), Author)); }
public async Task <IActionResult> Edit(int id, CreateOrUpdateAuthorViewModel Author) { if (id != Author.Id) { return(NotFound()); } if (ModelState.IsValid) { Author.Alias = Author.Name.ToFriendlyUrl(); var viewModel = _mapper.Map <Author>(Author); _context.Update(viewModel); Alert("Lưu danh mục thành công!"); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(nameof(CreateOrUpdate), Author)); }