public IActionResult UpdatePost(SchoolPostViewModel model, IFormFile imgFile) { model.SchoolId = HttpContext.Session.GetAuth().ActiveSchool; if (imgFile != null) { var filePath = Path.Combine("wwwroot/UploadFile/", Path.GetRandomFileName()).Replace(".", "") + imgFile.FileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } using (var localFile = System.IO.File.OpenWrite(filePath)) using (var uploadedFile = imgFile.OpenReadStream()) { uploadedFile.CopyTo(localFile); } model.ImgUrl = filePath; } if (model.Id != null && model.Id > 0) { _schoolPostServices.Update(model); } else { _schoolPostServices.Add(model); } return(RedirectToAction("Index")); }
public ResultModel Update(SchoolPostViewModel model) { var entityData = _context.SchoolPost.Find(model.Id); // entityData.PublishDate entityData.Content = model.Content; entityData.Title = model.Title; entityData.TypeId = model.TypeId; entityData.PublishDate = model.PublishDate; entityData.TerminationDate = model.TerminationDate; if (!string.IsNullOrEmpty(model.ImgUrl)) { entityData.ImgUrl = model.ImgUrl; } var result = _context.SaveChanges(); return(new ResultModel { Result = result > 0 ? ResultModelType.Success : ResultModelType.Error }); }
public ResultModel <int> Add(SchoolPostViewModel model) { var entityData = new SchoolPost { Content = model.Content, Title = model.Title, SchoolId = model.SchoolId, TypeId = model.TypeId, CreateDate = DateTime.Now, PublishDate = model.PublishDate, TerminationDate = model.TerminationDate, ImgUrl = model.ImgUrl }; _context.SchoolPost.Add(entityData); _context.SaveChanges(); return(new ResultModel <int> { Data = entityData.Id, Result = ResultModelType.Success }); }