public static IHtmlContent GenerateHtml <TModel, TProperty>( IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > propertyLambdaExpression, FieldsetViewModel fieldsetOptions = null, HintViewModel hintOptions = 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); 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("/GovUkDesignSystemComponents/Radios.cshtml", radiosViewModel)); }
public static IHtmlContent GovUkRadiosFor <TModel, TProperty>( this IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > propertyLambdaExpression, FieldsetViewModel fieldsetOptions = null, HintViewModel hintOptions = null) where TModel : GovUkViewModel { return(RadiosHtmlGenerator.GenerateHtml( htmlHelper, propertyLambdaExpression, fieldsetOptions, hintOptions)); }
public static IHtmlContent GovUkCheckboxesFor <TModel, TPropertyListItem>( this 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 // A fairly good check that TPropertyListItem is an Enum { return(CheckboxesHtmlGenerator.GenerateHtml( htmlHelper, propertyLambdaExpression, fieldsetOptions, hintOptions, conditionalOptions)); }
public static IHtmlContent GovUkRadiosFor <TModel, TProperty>( this IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > propertyLambdaExpression, FieldsetViewModel fieldsetOptions = null, HintViewModel hintOptions = null, Dictionary <TProperty, LabelViewModel> itemLabelOptions = null, Dictionary <TProperty, HintViewModel> itemHintOptions = null, string classes = null, Dictionary <TProperty, Func <object, object> > conditionalOptions = null) where TModel : GovUkViewModel { return(RadiosHtmlGenerator.GenerateHtml( htmlHelper, propertyLambdaExpression, fieldsetOptions, hintOptions, itemLabelOptions, itemHintOptions, conditionalOptions, classes)); }
public static IHtmlContent GovUkFieldset( this IHtmlHelper htmlHelper, FieldsetViewModel fieldsetViewModel) { return(htmlHelper.Partial("/GovUkDesignSystemComponents/Fieldset.cshtml", fieldsetViewModel)); }
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)); }
public static IHtmlContent GovUkFieldset( this IHtmlHelper htmlHelper, FieldsetViewModel fieldsetViewModel) { return(htmlHelper.Partial("~/Partials/Fieldset.cshtml", fieldsetViewModel)); }