public ActionResult Edit(TranslationInput input) { //TODO: still need some improvements if (!ModelState.IsValid) { return(PartialView("EditTranslation", input)); } input.Votes = 1; var updateMode = ITranslationMode.GetTranslationModeByID(input.TextId, input.LanguageCode); if (updateMode == null)//This mean it is the first Translation of this string { TranslationMode mode = new TranslationMode(); mode.TextId = input.TextId; mode.LanguageCode = input.LanguageCode; mode.Mode = input.ModeOfTranslation; ITranslationMode.InsertTranslationMode(mode); updateMode = mode; if (input.ModeOfTranslation == 2) { var griddata = this.Gridformat(); var gridrowData = griddata.GridRows.Where(x => x.TextId == input.TextId).FirstOrDefault(); return(Json(MapToGridModel(gridrowData))); } } else { updateMode.Mode = input.ModeOfTranslation; ITranslationMode.UpdateTranslationMode(updateMode); if (updateMode.Mode == 2) { var updateGriddata = this.Gridformat(); var updateGridrowData = updateGriddata.GridRows.Where(x => x.TextId == input.TextId).FirstOrDefault(); return(Json(MapToGridModel(updateGridrowData))); } } var newtranslation = new Translation(); if (updateMode.Mode == 0 || updateMode.Mode == 1) { var repetTranslated = ITranslation.GetAll().Where(x => x.TextId == input.TextId && x.LanguageCode == input.LanguageCode && x.Translated_Text == input.TranslationText).FirstOrDefault(); if (repetTranslated != null) { if (updateMode.Mode == 0) { var items = ITranslation.GetTranslationLogByCode(repetTranslated.TextId, repetTranslated.LanguageCode).ToList(); foreach (var item in items) { item.OfficialBoolean = false; } var maxVote = items.Max(s => s.Votes); var setdata = items.Where(x => x.Votes == maxVote).FirstOrDefault(); setdata.OfficialBoolean = true; setdata.Votes = maxVote; ITranslation.Update(setdata); } return(Json(repetTranslated)); } //Find the las translation that has officialBoolean in true var translatedData = ITranslation.GetAll() .Where(x => x.TextId == input.TextId && x.LanguageCode == input.LanguageCode && x.OfficialBoolean == true) .FirstOrDefault(); if (translatedData != null) { translatedData.OfficialBoolean = false; translatedData.Votes = translatedData.Votes > 0 ? 0 : translatedData.Votes; //Update the last saved translation with the new values in order to put the new translation as the one to be shown in the grid. // translateData is NOT the new input to be saved, is the last saved in a previous instance ITranslation.Update(translatedData); } // the new translation to be saved in db var translatedText = new Translation(); translatedText.Translated_Text = input.TranslationText; translatedText.LanguageCode = input.LanguageCode; translatedText.TextId = input.TextId; translatedText.Votes = 0; translatedText.OfficialBoolean = true; ITranslation.Save(translatedText); newtranslation = ITranslation.GetAll().Where(x => x.Id == translatedText.Id).FirstOrDefault(); this.VoteCount("Like", newtranslation.TextId, newtranslation.Id, newtranslation.LanguageCode); TranslationLog translation_log = new TranslationLog(); translation_log.TextId = translatedText.TextId; translation_log.System_Date = DateTime.Now; translation_log.LanguageCode = translatedText.LanguageCode; translation_log.Translated_Text = translatedText.Translated_Text; ITranslation_Log.Save(translation_log); } var data = this.Gridformat(); var rowData = data.GridRows.Where(x => x.TextId == input.TextId).FirstOrDefault(); if (System.Web.HttpContext.Current.Request.Cookies["MissingTrans"] != null) { if (System.Web.HttpContext.Current.Request.Cookies["MissingTrans"].Value != "true") { return(Json(MapToGridModel(rowData))); } else { return(Json(data)); } } else { return(Json(data)); } }