public async Task <IActionResult> AddEdit(int Id) { var model = new SmartProjectViewModel() { }; if (Id > 0) { var rec = _context.SmartCityProjects.Where(e => e.Id == Id).Select(k => new SmartProjectViewModel() { Id = k.Id, Description = k.Description, Name = k.Name, ImgId = k.DocumentId ?? 0, ImageName = _context.Documents.Where(d => d.Id == k.DocumentId).Select(m => m.FileName).FirstOrDefault(), Url = _context.Documents.Where(d => d.Id == k.DocumentId).Select(m => m.URL).FirstOrDefault(), DisplayLocation = k.DisplayLocation }).FirstOrDefault(); if (model != null) { model = rec; } } await SetUserData(); var DisplayLocations = from Enums.SmartCityProjectDisplayLocation es in Enum.GetValues(typeof(Enums.SmartCityProjectDisplayLocation)) select new { Id = es, Name = es.ToString() }; ViewBag.DisplayLocations = new SelectList(DisplayLocations, "Id", "Name"); return(View(model)); }
public async Task <IActionResult> Save(SmartProjectViewModel model) { //await SetUserData(); var user = await _userManager.GetUserAsync(User); if (ModelState.IsValid) { using (var transaction = _context.Database.BeginTransaction()) { try { if (model.Id > 0) { var rec = _context.SmartCityProjects.Where(e => e.Id == model.Id).FirstOrDefault(); if (rec == null) { throw new Exception("Record not found."); } Document imageRes = null; if (model.Image != null) { imageRes = await _documentManager.Save(model.Image, _amazonSettings.SliderBucketName); if (imageRes != null) { imageRes.DocumentCategory = Enums.DocumentCategory.SmartProjectImage; imageRes.CreatedBy = user.Id; rec.DocumentId = imageRes != null ? imageRes.Id : 0; } } rec.ModifiedBy = user.Id; rec.ModifiedOn = DateTime.UtcNow; rec.DisplayLocation = model.DisplayLocation; rec.Description = model.Description; rec.Name = model.Name; _context.Update(rec); _context.SaveChanges(); transaction.Commit(); return(RedirectToAction("Index")); } else { if (model.Image != null) { //Upload files var imageRes = await _documentManager.Save(model.Image, _amazonSettings.SliderBucketName); if (imageRes != null) { imageRes.DocumentCategory = Enums.DocumentCategory.SmartProjectImage; imageRes.CreatedBy = user.Id; _context.Add(new SmartCityProject() { CreatedBy = user.Id, CreatedOn = DateTime.UtcNow, Description = model.Description, Name = model.Name, DisplayLocation = model.DisplayLocation, DocumentId = imageRes.Id, }); _context.SaveChanges(); transaction.Commit(); return(RedirectToAction("Index")); } else { transaction.Rollback(); return(View("AddEdit", model)); //return Json(new { success = false, message = "Upload failed." }); } } else { ModelState.AddModelError("", "Please upload the Image"); } } } catch (Exception e) { transaction.Rollback(); ModelState.AddModelError("", e.Message); return(View("AddEdit", model)); // return Json(new { success = false, message = e.Message }); } } } return(View("AddEdit", model)); }