Example #1
0
        public void AddModelError(string key, string message)
        {
            var e = new ModelStateError(key, message);

            var mv = GetValue(key);
            if (mv == null)
            {
                mv = new ModelValue();
                mv.Name = key;
                Values.Add(key, mv);
            }

            mv.Errors.Add(e);
            Errors.Add(e);
        }
Example #2
0
        ModelValue GetModelValue(string key, IModelValueProvider provider, PropertyInfo pi)
        {
            // Get the model value
            var string_value = provider.GetValue(key);
            if (string_value == null)
                return null;

            // Create the model value entry
            var mv = new ModelValue();
            mv.Name = key;
            mv.InputValue = string_value;

            // Convert to required type
            if (pi.PropertyType != typeof(string))
            {
                try
                {
                    mv.ConvertedValue = Convert.ChangeType(string_value, pi.PropertyType);
                }
                catch (Exception x)
                {
                    mv.Errors.Add(new ModelStateError(pi.Name, string.Format("Can't convert '{0}' to {1} - {2}", string_value, pi.PropertyType.FullName, x.Message)));
                }
            }
            else
            {
                mv.ConvertedValue = string_value;
            }

            return mv;
        }