Esempio n. 1
0
        internal static IList <SelectListItem> EnumOptions(Type enumType, XcstWriter output, string?formatString = null, bool applyFormatInEdit = false)
        {
            Debug.Assert(enumType.IsEnum);

            var selectList = new List <SelectListItem>();

            const BindingFlags BindingFlags = BindingFlags.DeclaredOnly
                                              | BindingFlags.GetField
                                              | BindingFlags.Public
                                              | BindingFlags.Static;

            foreach (FieldInfo field in enumType.GetFields(BindingFlags))
            {
                object enumValue = field.GetValue(null);

                string value = (formatString != null && applyFormatInEdit) ?
                               String.Format(CultureInfo.CurrentCulture, formatString, enumValue)
               : field.Name;

                string text = (formatString != null && !applyFormatInEdit) ?
                              output.SimpleContent.Format(formatString, enumValue)
               : DisplayNameUtil.GetFieldDisplayName(field);

                selectList.Add(new SelectListItem {
                    Value = value,
                    Text  = text,
                });
            }

            return(selectList);
        }
Esempio n. 2
0
        public static void EnumTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData      = html.ViewData;
            ModelMetadata      modelMetadata = viewData.ModelMetadata;

            if (modelMetadata.Model != null)
            {
                if (modelMetadata.EditFormatString != null)
                {
                    // undo formatting if applicable to edit mode, for consistency with editor template
                    viewData.TemplateInfo.FormattedModelValue = modelMetadata.Model;
                }

                if (viewData.TemplateInfo.FormattedModelValue == modelMetadata.Model)
                {
#if ASPNETMVC
                    Type modelType = modelMetadata.ModelType;
                    Type enumType  = Nullable.GetUnderlyingType(modelType) ?? modelType;

                    if (enumType.IsEnum)
                    {
                        FieldInfo?enumField = enumType.GetField(modelMetadata.Model.ToString());

                        if (enumField != null)
                        {
                            viewData.TemplateInfo.FormattedModelValue =
                                DisplayNameUtil.GetFieldDisplayName(enumField);
                        }
                    }
#else
                    viewData.TemplateInfo.FormattedModelValue = modelMetadata.SimpleDisplayText;
#endif
                }
            }

            StringTemplate(html, package, seqOutput);
        }