public async Task <ActionResult> Edit(int?id) { if (id > 0) { var vCase = await _db.AchievementCategories.FindAsync(id); if (vCase == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR, JsonRequestBehavior.AllowGet)); } var editCase = _mapper.Map <AchievementCategory, AchievementCategoryIM>(vCase); var pageMeta = await _db.PageMetas.FirstOrDefaultAsync(d => d.ModelType == (short)ModelType.ACHIEVEMENTCATEGORY && d.ObjectId == editCase.Id.ToString()); if (pageMeta != null) { editCase.SEOTitle = pageMeta.Title; editCase.Keywords = pageMeta.Keyword; editCase.SEODescription = pageMeta.Description; } return(View(editCase)); } else { var vm = new AchievementCategoryIM { Active = true, Importance = 0 }; return(View(vm)); } }
public async Task <JsonResult> Edit(AchievementCategoryIM vm) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR, JsonRequestBehavior.DenyGet)); } try { if (vm.Id > 0) { var editCase = await _db.AchievementCategories.FindAsync(vm.Id); editCase = _mapper.Map(vm, editCase); editCase.UpdatedDate = DateTime.Now; editCase.UpdatedBy = Site.CurrentUserName; _db.Entry(editCase).State = EntityState.Modified; await _db.SaveChangesAsync(); var pageMeta = await _db.PageMetas.FirstOrDefaultAsync(d => d.ModelType == (short)ModelType.ACHIEVEMENTCATEGORY && d.ObjectId == editCase.Id.ToString()); pageMeta = pageMeta ?? new PageMeta(); pageMeta.ObjectId = vm.Id.ToString(); pageMeta.Title = string.IsNullOrEmpty(vm.Title) ? vm.Title : vm.Title; pageMeta.Keyword = string.IsNullOrEmpty(vm.Keywords) ? vm.Title : vm.Keywords.Replace(',', ','); pageMeta.Description = vm.SEODescription; pageMeta.ModelType = (short)ModelType.ACHIEVEMENTCATEGORY; if (pageMeta.Id > 0) { _db.Entry(pageMeta).State = EntityState.Modified; } else { _db.PageMetas.Add(pageMeta); } await _db.SaveChangesAsync(); AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, EntityNames.Case)); return(Json(AR, JsonRequestBehavior.DenyGet)); } else { var newCase = _mapper.Map <AchievementCategoryIM, AchievementCategory>(vm); newCase.CreatedDate = DateTime.Now; newCase.CreatedBy = Site.CurrentUserName; newCase = _db.AchievementCategories.Add(newCase); var result = await _db.SaveChangesAsync(); if (result > 0) { if (!string.IsNullOrEmpty(vm.Keywords) || !string.IsNullOrEmpty(vm.SEODescription)) { var pm = new PageMeta { Title = vm.SEOTitle, Description = vm.SEODescription, Keyword = vm.Keywords, ModelType = (short)ModelType.ACHIEVEMENTCATEGORY, ObjectId = newCase.Id.ToString() }; _db.PageMetas.Add(pm); await _db.SaveChangesAsync(); } } AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, EntityNames.Case)); return(Json(AR, JsonRequestBehavior.DenyGet)); } } catch (Exception er) { AR.Setfailure(er.Message); return(Json(AR, JsonRequestBehavior.DenyGet)); } // if (!ModelState.IsValid) // { // AR.Setfailure(GetModelErrorMessage()); // return Json(AR, JsonRequestBehavior.DenyGet); // } // //var newCategory = _categoryServices.GetById(vm.Id); // //newCategory.Title = vm.Title; // //newCategory.SeoName = vm.SeoName; // //newCategory.Importance = vm.Importance; // //newCategory.Active = vm.Active; // //newCategory.UpdatedBy = Site.CurrentUserName; // //newCategory.UpdatedDate = DateTime.Now; // var model = _db.AchievementCategories.Find(vm.Id); // model = _mapper.Map(vm, model); // _db.Entry(model).State = EntityState.Modified; // // _db.SaveChanges(); // // _categoryServices.Update(newCategory); // var pageMeta = _db.PageMetas.FirstOrDefault(d => d.ModelType == (short)ModelType.ARTICLECATEGORY && d.ObjectId == vm.Id.ToString()); // //_pageMetaServices.GetPageMeta(ModelType.ARTICLECATEGORY, vm.Id.ToString()); // pageMeta = pageMeta ?? new PageMetaSet(); // pageMeta.ObjectId = vm.Id.ToString(); // pageMeta.Title = string.IsNullOrEmpty(vm.SEOTitle) ? vm.Title : vm.SEOTitle; // pageMeta.Keyword = string.IsNullOrEmpty(vm.Keywords) ? vm.Title : vm.Keywords.Replace(',', ','); // pageMeta.Description = vm.SEODescription; // pageMeta.ModelType = (short)ModelType.ARTICLECATEGORY; // if (pageMeta.Id > 0) // { // _db.Entry(pageMeta).State = EntityState.Modified; // //_pageMetaServices.Update(pageMeta); // } // else // { // _db.PageMetas.Add(pageMeta); // //_pageMetaServices.Create(pageMeta); // } // _db.SaveChanges(); //// var category = _mapper.Map<AchievementCategoryVM>(newCategory); // AR.Id = model.Id; // AR.Data = RenderPartialViewToString("_CategoryItem", model); // AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, EntityNames.Category)); // return Json(AR, JsonRequestBehavior.DenyGet); }