public ActionResult Update([DataSourceRequest] DataSourceRequest dsRequest, QuestionDTO questionDTO) { try { //check to ensure the user owns the resources she is trying to access. if not; we get out of here. //Somebody is trying to do bad stuff. if (!ProbeValidate.IsQuestionForLoggedInUser((long)questionDTO.Id)) { ModelState.AddModelError("", "Question Update could not be accomplished"); return(Json(ModelState.ToDataSourceResult())); } ValidateQuestionEdit(questionDTO); if (ModelState.IsValid) { ChoiceQuestion choiceQuestion = db.ChoiceQuestion.Find(questionDTO.Id); //choiceQuestion.AspNetUsersId - THIS IS NOT CHANGING choiceQuestion.QuestionTypeId = questionDTO.QuestionTypeId; choiceQuestion.Name = questionDTO.Name; choiceQuestion.Text = questionDTO.Text; choiceQuestion.Tags = questionDTO.Tags; //choiceQuestion.OneChoice - THIS IS NOT CHANGING //choiceQuestion.UsedInGame - THIS IS NOT CHANGING //choiceQuestion.ACLId - THIS IS NOT CHANGING db.Entry(choiceQuestion).State = EntityState.Modified; db.SaveChanges(Request != null ? Request.LogonUserIdentity.Name : null); } //return Json(ModelState.ToDataSourceResult()); return(Json(new[] { questionDTO }.ToDataSourceResult(dsRequest, ModelState))); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); //log to elmah ModelState.AddModelError("", ProbeConstants.MSG_UnsuccessfulOperation_STR); return(Json(ModelState.ToDataSourceResult())); } }//public ActionResult Update([DataSourceRequest] DataSourceRequest dsRequest, QuestionDTO questionDTO)
public ActionResult Update([DataSourceRequest] DataSourceRequest dsRequest, GameConfigurationDTO gameConfigurationDTO) { try { long gameId = (long)gameConfigurationDTO.GameId; //check to ensure the user owns the resources she is trying to access. if not; we get out of here. //Somebody is trying to do bad stuff. if (!ProbeValidate.IsGameForLoggedInUser((long)gameConfigurationDTO.GameId)) { ModelState.AddModelError("", "Game Configuration Update could not be accomplished"); return(Json(ModelState.ToDataSourceResult())); } if (ModelState.IsValid) { switch (gameConfigurationDTO.DataTypeG) { case ConfigurationG.ProbeDataType.TEXT: { if (gameConfigurationDTO.Value.Contains("script")) { ModelState.AddModelError("Value", "Value entered is invalid"); return(Json(ModelState.ToDataSourceResult())); } else if (gameConfigurationDTO.Value.Contains("$(")) { ModelState.AddModelError("Value", "Value entered is invalid"); return(Json(ModelState.ToDataSourceResult())); } else if (gameConfigurationDTO.Value.ToLower().Contains("delete")) { ModelState.AddModelError("Value", "Value entered is invalid"); return(Json(ModelState.ToDataSourceResult())); } break; } case ConfigurationG.ProbeDataType.INT: { int valueOutput; if (!Int32.TryParse(gameConfigurationDTO.Value, out valueOutput)) { ModelState.AddModelError("Value", "Value entered is not an integer"); return(Json(ModelState.ToDataSourceResult())); } break; } case ConfigurationG.ProbeDataType.FLOAT: { double valueOutput; if (!Double.TryParse(gameConfigurationDTO.Value, out valueOutput)) { ModelState.AddModelError("Value", "Value entered is not a real"); return(Json(ModelState.ToDataSourceResult())); } break; } case ConfigurationG.ProbeDataType.BOOLEAN: { if (!(gameConfigurationDTO.Value.IsCaseInsensitiveEqual("false") || gameConfigurationDTO.Value.IsCaseInsensitiveEqual("true"))) { ModelState.AddModelError("Value", "Value entered must be true or false"); return(Json(ModelState.ToDataSourceResult())); } else { gameConfigurationDTO.Value = gameConfigurationDTO.Value.ToLower(); } break; } } ; //Create a valid GameConfiguration record GameConfiguration gameConfiguration = new GameConfiguration { ConfigurationGId = gameConfigurationDTO.ConfigurationGId, GameId = (long)gameConfigurationDTO.GameId, Value = gameConfigurationDTO.Value }; /* * First we will check if there is an existing GameConfiguration record. If not, then we will create that record. If * it exists; then we will update that record. */ if (db.GameConfiguration .Where(gc => gc.ConfigurationGId == gameConfigurationDTO.ConfigurationGId && gc.GameId == gameConfigurationDTO.GameId).Count() == 0) { //GameConfiguration record doesn't exist; so we are going to create it. db.GameConfiguration.Add(gameConfiguration); db.SaveChanges(Request != null ? Request.LogonUserIdentity.Name : null); gameConfigurationDTO.Id = gameConfiguration.Id; } else { //to edit an existing record; you need to seed the id with the existing PK gameConfiguration.Id = (int)gameConfigurationDTO.Id; db.Entry(gameConfiguration).State = EntityState.Modified; db.SaveChanges(Request != null ? Request.LogonUserIdentity.Name : null); } }//if (ModelState.IsValid) //return Json(ModelState.ToDataSourceResult()); return(Json(new[] { gameConfigurationDTO }.ToDataSourceResult(dsRequest, ModelState))); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); //log to elmah ModelState.AddModelError("", ProbeConstants.MSG_UnsuccessfulOperation_STR); return(Json(ModelState.ToDataSourceResult())); } }//public ActionResult Update([DataSourceRequest] DataSourceRequest dsRequest, GameConfigurationDTO gameConfigurationDTO)