コード例 #1
0
 public static IHtmlContent GovUkHint(
     this IHtmlHelper htmlHelper,
     HintViewModel hintViewModel)
 {
     return(htmlHelper.Partial("/GovUkDesignSystemComponents/Hint.cshtml", hintViewModel));
 }
        public static IHtmlContent GenerateHtml <TModel, TPropertyListItem>(
            IHtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, List <TPropertyListItem> > > propertyLambdaExpression,
            FieldsetViewModel fieldsetOptions = null,
            HintViewModel hintOptions         = null,
            Dictionary <TPropertyListItem, Func <object, object> > conditionalOptions = null)
            where TModel : GovUkViewModel
            where TPropertyListItem : struct, IConvertible
        {
            PropertyInfo property = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression);

            ThrowIfPropertyTypeIsNotListOfEnums <TPropertyListItem>(property);
            string propertyName = property.Name;

            TModel model = htmlHelper.ViewData.Model;
            List <TPropertyListItem> currentlySelectedValues =
                ExpressionHelpers.GetPropertyValueFromModelAndExpression(model, propertyLambdaExpression);

            List <TPropertyListItem> allEnumValues =
                Enum.GetValues(typeof(TPropertyListItem))
                .Cast <TPropertyListItem>()
                .ToList();

            List <ItemViewModel> checkboxes = allEnumValues
                                              .Select(enumValue =>
            {
                var isEnumValueInListOfCurrentlySelectedValues =
                    currentlySelectedValues != null && currentlySelectedValues.Contains(enumValue);

                string checkboxLabelText = GetCheckboxLabelText(enumValue);

                var checkboxItemViewModel = new CheckboxItemViewModel
                {
                    Value   = enumValue.ToString(),
                    Id      = $"GovUk_Checkbox_{propertyName}_{enumValue}",
                    Checked = isEnumValueInListOfCurrentlySelectedValues,
                    Label   = new LabelViewModel
                    {
                        Text = checkboxLabelText
                    }
                };

                if (conditionalOptions != null && conditionalOptions.TryGetValue(enumValue, out Func <object, object> conditionalHtml))
                {
                    checkboxItemViewModel.Conditional = new Conditional {
                        Html = conditionalHtml
                    };
                }

                return(checkboxItemViewModel);
            })
                                              .Cast <ItemViewModel>()
                                              .ToList();

            var checkboxesViewModel = new CheckboxesViewModel
            {
                Name     = $"GovUk_Checkbox_{propertyName}",
                IdPrefix = $"GovUk_{propertyName}",
                Items    = checkboxes,
                Fieldset = fieldsetOptions,
                Hint     = hintOptions
            };

            if (model.HasErrorFor(property))
            {
                checkboxesViewModel.ErrorMessage = new ErrorMessageViewModel
                {
                    Text = model.GetErrorFor(property)
                };
            }

            return(htmlHelper.Partial("/GovUkDesignSystemComponents/Checkboxes.cshtml", checkboxesViewModel));
        }
コード例 #3
0
 public static IHtmlContent GovUkHint(
     this IHtmlHelper htmlHelper,
     HintViewModel hintViewModel)
 {
     return(htmlHelper.Partial("~/Partials/Hint.cshtml", hintViewModel));
 }
コード例 #4
0
        public static IHtmlContent GenerateHtml <TModel, TProperty>(
            IHtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > propertyLambdaExpression,
            FieldsetViewModel fieldsetOptions = null,
            HintViewModel hintOptions         = null)
            where TModel : GovUkViewModel
        {
            var property = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression);

            ThrowIfPropertyTypeIsNotNullableEnum(property);
            var propertyName = property.Name;

            var model = htmlHelper.ViewData.Model;
            var currentlySelectedValue =
                ExpressionHelpers.GetPropertyValueFromModelAndExpression(model, propertyLambdaExpression);

            var enumType      = Nullable.GetUnderlyingType(typeof(TProperty));
            var allEnumValues = Enum.GetValues(enumType);


            var radios = allEnumValues
                         .OfType <object>()
                         .Select(enumValue =>
            {
                var isEnumValueCurrentlySelected = enumValue.ToString() == currentlySelectedValue.ToString();
                var radioLabelText = GetRadioLabelText(enumType, enumValue);

                return(new RadioItemViewModel
                {
                    Value = enumValue.ToString(),
                    Id = $"GovUk_Radio_{propertyName}_{enumValue}",
                    Checked = isEnumValueCurrentlySelected,
                    Label = new LabelViewModel
                    {
                        Text = radioLabelText
                    }
                });
            })
                         .Cast <ItemViewModel>()
                         .ToList();

            var radiosViewModel = new RadiosViewModel
            {
                Name     = $"GovUk_Radio_{propertyName}",
                IdPrefix = $"GovUk_{propertyName}",
                Items    = radios,
                Fieldset = fieldsetOptions,
                Hint     = hintOptions
            };

            if (model.HasErrorFor(property))
            {
                radiosViewModel.ErrorMessage = new ErrorMessageViewModel
                {
                    Text = model.GetErrorFor(property)
                }
            }
            ;

            return(htmlHelper.Partial("~/Partials/Radios.cshtml", radiosViewModel));
        }
コード例 #5
0
        public static IHtmlContent GenerateHtml <TModel, TProperty>(IHtmlHelper <TModel> htmlHelper,
                                                                    Expression <Func <TModel, TProperty> > propertyLambdaExpression,
                                                                    FieldsetViewModel fieldsetOptions = null,
                                                                    HintViewModel hintOptions         = null,
                                                                    Dictionary <TProperty, LabelViewModel> itemLabelOptions           = null,
                                                                    Dictionary <TProperty, HintViewModel> itemHintOptions             = null,
                                                                    Dictionary <TProperty, Func <object, object> > conditionalOptions = null,
                                                                    string classes = null)
            where TModel : GovUkViewModel
        {
            PropertyInfo property = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression);

            ThrowIfPropertyTypeIsNotNullableEnum(property);
            string propertyName = property.Name;

            TModel    model = htmlHelper.ViewData.Model;
            TProperty currentlySelectedValue =
                ExpressionHelpers.GetPropertyValueFromModelAndExpression(model, propertyLambdaExpression);

            Type  enumType      = Nullable.GetUnderlyingType(typeof(TProperty));
            Array allEnumValues = Enum.GetValues(enumType);


            List <ItemViewModel> radios = allEnumValues
                                          .OfType <object>()
                                          .Select(enumValue =>
            {
                bool isEnumValueCurrentlySelected = enumValue.ToString() == currentlySelectedValue.ToString();
                string radioLabelText             = GetRadioLabelText(enumType, enumValue);

                var radioItemViewModel = new RadioItemViewModel
                {
                    Value   = enumValue.ToString(),
                    Id      = $"GovUk_Radio_{propertyName}_{enumValue}",
                    Checked = isEnumValueCurrentlySelected,
                    Label   = new LabelViewModel()
                };

                if (conditionalOptions != null && conditionalOptions.TryGetValue((TProperty)enumValue, out Func <object, object> conditionalHtml))
                {
                    radioItemViewModel.Conditional = new Conditional {
                        Html = conditionalHtml
                    };
                }

                if (itemLabelOptions != null && itemLabelOptions.TryGetValue((TProperty)enumValue, out LabelViewModel labelViewModel))
                {
                    radioItemViewModel.Label = labelViewModel;
                }
                if (radioItemViewModel.Label.Text == null && radioItemViewModel.Label.Html == null)
                {
                    radioItemViewModel.Label.Text = radioLabelText;
                }

                if (itemHintOptions != null && itemHintOptions.TryGetValue((TProperty)enumValue, out HintViewModel hintViewModel))
                {
                    radioItemViewModel.Hint = hintViewModel;
                }

                return(radioItemViewModel);
            })
                                          .Cast <ItemViewModel>()
                                          .ToList();

            var radiosViewModel = new RadiosViewModel
            {
                Name     = $"GovUk_Radio_{propertyName}",
                IdPrefix = $"GovUk_{propertyName}",
                Items    = radios,
                Fieldset = fieldsetOptions,
                Hint     = hintOptions,
                Classes  = classes
            };

            if (model.HasErrorFor(property))
            {
                radiosViewModel.ErrorMessage = new ErrorMessageViewModel
                {
                    Text = model.GetErrorFor(property)
                };
            }

            return(htmlHelper.Partial("/GovUkDesignSystemComponents/Radios.cshtml", radiosViewModel));
        }
コード例 #6
0
 public SettingSortingOrderVM(string displayText)
 {
     SettingHint = new HintViewModel(new TextObject(displayText));
     Name        = displayText;
 }