Exemplo n.º 1
0
        public ActionResult SubmitRegistration(RegistrationViewModel customerModel)
        {
            var config          = _fileManager.LoadContentAs <IEnumerable <FieldConfig> >(TemplateFilePath);
            var resultingFields = new Dictionary <string, string>();

            foreach (var field in config)
            {
                //remove errors for any field that is not visible
                if (!field.IsVisible && ModelState[field.Name] != null && ModelState[field.Name].Errors.Any())
                {
                    ModelState[field.Name].Errors.Clear();
                    continue;
                }


                var property = customerModel.GetType().GetProperty(field.Name);
                var value    = string.Empty;

                if (field.IsVisible && property != null)
                {
                    if (property?.PropertyType.Name == "String")
                    {
                        value = (string)(property?.GetValue(customerModel, null));
                    }
                    else if (property?.PropertyType.Name == "DateTime")
                    {
                        value = ((DateTime)(property?.GetValue(customerModel, null))).ToString("d");
                    }
                    resultingFields.Add(field.Name, value);
                }
            }

            //if model is still not valid, we need to not continue
            if (!ModelState.IsValid)
            {
                customerModel.Config = config;
                return(View("FillForm", customerModel));
            }


            //display result
            return(View("ThankYou", resultingFields));
        }