public ActionResult Create(Template template) { ModelState.Clear(); var newTemplate = new Template(template.BodyText, template.TemplateType, template.Ceremony); newTemplate.Subject = template.Subject; newTemplate.TransferValidationMessagesTo(ModelState); // validate the tokens var tokenValidation = template.BodyText != null ? ValidateBody(newTemplate) : null; if (!string.IsNullOrEmpty(tokenValidation)) ModelState.AddModelError("template.BodyText", tokenValidation); // get any existing ones var oldTemplates = template.Ceremony.Templates.Where(a => a.TemplateType == template.TemplateType && a.IsActive); if (ModelState.IsValid) { foreach (var a in oldTemplates) { a.IsActive = false; Repository.OfType<Template>().EnsurePersistent(a); } Repository.OfType<Template>().EnsurePersistent(newTemplate); return this.RedirectToAction(a => a.Index(newTemplate.Ceremony.Id)); } var viewModel = TemplateCreateViewModel.Create(Repository, newTemplate, newTemplate.Ceremony); return View(viewModel); }
private string ValidateBody(Template template) { var invalid = new List<string>(); var message = string.Empty; if (_letterGenerator.ValidateTemplate(template, invalid)) { message = string.Empty; } else { message = string.Format("Template has {0} invalid token(s). The following are invalid: {1}", invalid.Count, string.Join(", ", invalid)); } return message; }