public ActionResult AddScreeningCriteriaToTrial(AddScreeningCriteriaToTrialViewModel m) { m.OperatorValue = m.OperatorValue.Trim(); if (m.OperatorType != PX_Model.Screening_Criteria.OPERATOR_EQUALS) { if (m.OperatorType == PX_Model.Screening_Criteria.OPERATOR_RANGE) { string[] words = m.OperatorValue.Split(','); if (words.Count() != 2) { ModelState.AddModelError("Add", "Range must have 2 numeric values separated by comma. Eg: 10,50"); } else { decimal value1, value2; words[0] = words[0].Trim(); words[1] = words[1].Trim(); bool r1 = decimal.TryParse(words[0], out value1); bool r2 = decimal.TryParse(words[1], out value2); if (!r1 || !r2) { ModelState.AddModelError("Add", "Both range values must be numeric"); } } } else { decimal value; bool retVal = decimal.TryParse(m.OperatorValue, out value); if (!retVal) { ModelState.AddModelError("Add", "Operator value must be numeric value"); } } var options = _db.Screening_Criteria_Option.Where(o => o.Screening_Criteria_Id == m.ScreeningCriteriaId); foreach (var option in options) { decimal value; bool retVal = decimal.TryParse(option.Description.Trim(), out value); if (!retVal) { ModelState.AddModelError("Add", "One or more options in this selection criteria is non numeric. You can only choose 'Equals' as operator type."); break; } } } if (ModelState.IsValid) { var tsc = new PX_Model.Trial_Screening_Criteria(); tsc.Trial_Id = m.TrialId; tsc.Screening_Criteria_Id = m.ScreeningCriteriaId; tsc.OperatorType = m.OperatorType; tsc.OperatorValue = m.OperatorValue; _db.Trial_Screening_Criteria.Add(tsc); _db.SaveChanges(); return View("ScreeningCriteria"); } m.Trials = _db.Trials.OrderBy(t => t.Name).ToArray(); m.ScreeningCriteria = _db.Screening_Criteria.OrderBy(s => s.Description).ToArray(); return View(m); }
public ActionResult AddScreeningCriteriaToTrial(AddScreeningCriteriaToTrialViewModel m) { //Get rid of spaces at the front and back of operator value m.OperatorValue = m.OperatorValue.Trim(); if (m.OperatorType != PX_Model.Screening_Criteria.OPERATOR_EQUALS) { if (m.OperatorType == PX_Model.Screening_Criteria.OPERATOR_RANGE) { //Get the two values for the range string[] words = m.OperatorValue.Split(','); if (words.Count() != 2) { ModelState.AddModelError("Add", "Range must have 2 numeric values separated by comma. Eg: 10,50"); } else { decimal value1, value2; words[0] = words[0].Trim(); words[1] = words[1].Trim(); //Get the two values as decimal bool r1 = decimal.TryParse(words[0], out value1); bool r2 = decimal.TryParse(words[1], out value2); if (!r1 || !r2) { ModelState.AddModelError("Add", "Both range values must be numeric"); } } } else { //Get the numerical value of the input decimal value; bool retVal = decimal.TryParse(m.OperatorValue, out value); if (!retVal) { ModelState.AddModelError("Add", "Operator value must be numeric value"); } } //Get all the options and make sure they are numeric as well var options = _db.Screening_Criteria_Option.Where(o => o.Screening_Criteria_Id == m.ScreeningCriteriaId); foreach (var option in options) { decimal value; bool retVal = decimal.TryParse(option.Description.Trim(), out value); if (!retVal) { ModelState.AddModelError("Add", "One or more options in this selection criteria is non numeric. You can only choose 'Equals' as operator type."); break; } } } //If everything's alright if (ModelState.IsValid) { //Add the screening criteria to the database var tsc = new PX_Model.Trial_Screening_Criteria(); tsc.Trial_Id = m.TrialId; tsc.Screening_Criteria_Id = m.ScreeningCriteriaId; tsc.OperatorType = m.OperatorType; tsc.OperatorValue = m.OperatorValue; _db.Trial_Screening_Criteria.Add(tsc); _db.SaveChanges(); return View("ScreeningCriteria"); } //Else get the list of trial and screening criteria again and show the form with error messages m.Trials = _db.Trials.OrderBy(t => t.Name).ToArray(); m.ScreeningCriteria = _db.Screening_Criteria.OrderBy(s => s.Description).ToArray(); return View(m); }