Пример #1
0
 /// <summary>
 /// Creates a new Radio control.
 /// </summary>
 /// <param name="requestContext">The request helper.</param>
 /// <param name="model">The current model.</param>
 /// <param name="metadata">The model metadata.</param>
 public FWRadioControl(FWRequestContext requestContext, object model, ModelMetadata metadata)
     : base(requestContext, model, metadata)
 {
     if (FWReflectionHelper.CheckType <bool>(metadata.ModelType))
     {
         _isBoolean = true;
         _values    = new Dictionary <string, string>
         {
             { "true", GetModelResource("TRUE") },
             { "false", GetModelResource("FALSE") }
         };
         if (model != null)
         {
             _selected = model.ToString();
         }
     }
     else if (FWReflectionHelper.IsEnum(metadata.ModelType))
     {
         var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType);
         _values = new Dictionary <string, string>();
         // Gets the resource values for the enum items.
         foreach (var item in enumDictionary)
         {
             _values.Add(item.Key, GetModelResource(item.Value));
         }
         _selected = FWEnumHelper.GetValues(model as Enum).FirstOrDefault();
     }
     else
     {
         throw new FWMissingDatasourceException(Id);
     }
 }
Пример #2
0
        private string GetPropertyValue()
        {
            switch (_modelType.Name)
            {
            case nameof(String):
                return(_model.ToString());

            case nameof(Boolean):
                return(GetCheckboxText());

            case nameof(DateTime):
                return(((DateTime)_model).ToString("d"));
            }

            if (FWReflectionHelper.IsEnum(_modelType))
            {
                if (_model == null)
                {
                    return(string.Empty);
                }
                return(FWStringLocalizer.GetModelResource($"{OriginalId}_{_model.ToString()}", _metadata.ContainerType?.Name));
            }

            if (_metadata.AdditionalValues.ContainsKey(nameof(FWSelectAttribute)))
            {
                return(GetSelectText());
            }

            return(_model.ToString());
        }
Пример #3
0
        private FWInputControl AttemptCreateControlByModelType()
        {
            if (_modelType == FWKnownTypes.Bool)
            {
                return(CreateCheckbox());
            }

            if (_modelType == FWKnownTypes.DateTime)
            {
                return(CreateDatepicker());
            }

            // Checks if the model is an enum.
            if (FWReflectionHelper.IsEnum(_modelType))
            {
                return(CreateSelect());
            }

            return(CreateTextbox());
        }
Пример #4
0
        /// <summary>
        /// Creates a new Checkbox control.
        /// </summary>
        /// <param name="requestContext">The request helper.</param>
        /// <param name="model">The current model.</param>
        /// <param name="metadata">The model metadata.</param>
        public FWCheckboxControl(FWRequestContext requestContext, object model, ModelMetadata metadata)
            : base(requestContext, model, metadata)
        {
            if (FWReflectionHelper.CheckType <bool>(metadata.ModelType))
            {
                _isBoolean = true;
                _values    = new Dictionary <string, string>
                {
                    { "true", DisplayName }
                };
                if (model != null)
                {
                    _selected.Add(model.ToString());
                }

                //If the property is boolean, there is no sense in being required.
                if (metadata.ModelType == FWKnownTypes.Bool)
                {
                    IsRequired = false;
                }

                HideLabel();
            }
            else if (FWReflectionHelper.IsEnum(metadata.ModelType))
            {
                var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType);
                _values = new Dictionary <string, string>();
                // Gets the resource values for the enum items.
                foreach (var item in enumDictionary)
                {
                    _values.Add(item.Key, GetModelResource(item.Value));
                }
                _selected = FWEnumHelper.GetValues(model as Enum).ToList();
            }
            else
            {
                throw new FWMissingDatasourceException(Id);
            }
        }
Пример #5
0
        /// <summary>
        /// Creates a new Select control.
        /// </summary>
        /// <param name="requestContext">The request helper.</param>
        /// <param name="model">The current model.</param>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="container">The model container object.</param>
        public FWSelectControl(FWRequestContext requestContext, object model, ModelMetadata metadata, object container = null)
            : base(requestContext, model, metadata)
        {
            if (metadata.UnderlyingOrModelType == FWKnownTypes.Bool)
            {
                _datasource = new List <IFWDatasourceItem>
                {
                    new FWDatasourceItem()
                    {
                        Id = "true", Value = GetModelResource("TRUE")
                    },
                    new FWDatasourceItem()
                    {
                        Id = "false", Value = GetModelResource("FALSE")
                    }
                };

                if (model != null)
                {
                    _selected = model.ToString();
                }
            }
            else if (FWReflectionHelper.IsEnum(metadata.ModelType))
            {
                var enumDictionary = FWEnumHelper.GetEnumAsDictonary(metadata.ModelType);
                var datasource     = new List <IFWDatasourceItem>();
                // Gets the resource values for the enum items.
                foreach (var item in enumDictionary)
                {
                    datasource.Add(new FWDatasourceItem()
                    {
                        Id = item.Key, Value = GetModelResource(item.Value)
                    });
                }
                _datasource = datasource;

                _selected = FWEnumHelper.GetValues(model as Enum).FirstOrDefault();
            }
            else
            {
                // Looks for the datasource inside the view model.
                _datasource = FWDatasourceHelper.GetDatasourceFromModel(_datasourceName, requestContext.Model);

                // If the datasource is not found, try to look for it inside the container object.
                if (_datasource == null)
                {
                    _datasource = FWDatasourceHelper.GetDatasourceFromModel(_datasourceName, container);
                }

                if (model != null)
                {
                    _selected = model.ToString();
                }
            }

            if (DataBind)
            {
                requestContext.HttpContext.AddDatasource(metadata.PropertyName, metadata.ContainerMetadata.ModelType, _datasource);
                _datasourceName = metadata.PropertyName;
            }

            _allowClear = !IsRequired;
        }