public static IHtmlContent GenerateHtml <TModel>(
            IHtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, bool> > propertyLambdaExpression,
            LabelViewModel labelOptions = null,
            HintViewModel hintOptions   = null,
            Conditional conditional     = null,
            bool disabled = false)
        {
            PropertyInfo property     = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression);
            string       propertyName = property.Name;

            TModel model     = htmlHelper.ViewData.Model;
            bool   isChecked = ExpressionHelpers.GetPropertyValueFromModelAndExpression(model, propertyLambdaExpression);

            if (labelOptions != null)
            {
                labelOptions.For = propertyName;
            }

            var checkboxItemViewModel = new CheckboxItemViewModel
            {
                Id          = propertyName,
                Name        = propertyName,
                Value       = true.ToString(),
                Label       = labelOptions,
                Hint        = hintOptions,
                Conditional = conditional,
                Disabled    = disabled,
                Checked     = isChecked
            };

            return(htmlHelper.Partial("/GovUkDesignSystemComponents/CheckboxItem.cshtml", checkboxItemViewModel));
        }
Ejemplo n.º 2
0
 public DateRangeInputViewModel(
     string id,
     string label,
     DateInputViewModel startDateModel,
     DateInputViewModel endDateModel,
     CheckboxItemViewModel endDateCheckboxViewModel,
     string?hintText = null
     )
 {
     Id              = id;
     Label           = label;
     StartDateModel  = startDateModel;
     EndDateModel    = endDateModel;
     EndDateCheckbox = endDateCheckboxViewModel;
     HintText        = hintText;
 }
        public IViewComponentResult Invoke(
            string aspFor,
            string label,
            string?hintText
            )
        {
            var model        = ViewData.Model;
            var property     = model.GetType().GetProperty(aspFor);
            var valueToSet   = (bool)(property?.GetValue(model) ?? false);
            var errorMessage = ViewData.ModelState[property?.Name]?.Errors?.FirstOrDefault()?.ErrorMessage;

            var viewModel = new CheckboxItemViewModel(
                aspFor,
                aspFor,
                label,
                valueToSet,
                hintText,
                errorMessage
                );

            return(View(viewModel));
        }
 public static IHtmlContent GovUkCheckboxItem(
     this IHtmlHelper htmlHelper,
     CheckboxItemViewModel checkboxItemViewModel)
 {
     return(htmlHelper.Partial("/GovUkDesignSystemComponents/CheckboxItem.cshtml", checkboxItemViewModel));
 }
        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));
        }
Ejemplo n.º 6
0
 public static IHtmlContent GovUkCheckboxItem(
     this IHtmlHelper htmlHelper,
     CheckboxItemViewModel checkboxItemViewModel)
 {
     return(htmlHelper.Partial("~/Partials/CheckboxItem.cshtml", checkboxItemViewModel));
 }
Ejemplo n.º 7
0
        /// <summary>
        ///     Render DateInput view component.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="label"></param>
        /// <param name="startDayId"></param>
        /// <param name="startMonthId"></param>
        /// <param name="startYearId"></param>
        /// <param name="endDayId"></param>
        /// <param name="endMonthId"></param>
        /// <param name="endYearId"></param>
        /// <param name="endDateCheckboxId"></param>
        /// <param name="endDateCheckboxLabel"></param>
        /// <param name="hintText">Leave blank for no hint.</param>
        /// <param name="endDateCheckboxHintText">Leave blank for no hint.</param>
        /// <returns></returns>
        public IViewComponentResult Invoke(
            string id,
            string label,
            string startDayId,
            string startMonthId,
            string startYearId,
            string endDayId,
            string endMonthId,
            string endYearId,
            string endDateCheckboxId,
            string endDateCheckboxLabel,
            string hintText,
            string endDateCheckboxHintText
            )
        {
            var model = ViewData.Model;

            var(startDayValue, startDayErrors)     = GetStringValueAndErrorsForProperty(model, startDayId);
            var(startMonthValue, startMonthErrors) = GetStringValueAndErrorsForProperty(model, startMonthId);
            var(startYearValue, startYearErrors)   = GetStringValueAndErrorsForProperty(model, startYearId);

            var(endDayValue, endDayErrors)     = GetStringValueAndErrorsForProperty(model, endDayId);
            var(endMonthValue, endMonthErrors) = GetStringValueAndErrorsForProperty(model, endMonthId);
            var(endYearValue, endYearErrors)   = GetStringValueAndErrorsForProperty(model, endYearId);

            var checkboxProperty = model.GetType().GetProperty(endDateCheckboxId);
            var checkboxValue    = (bool)checkboxProperty?.GetValue(model) !;

            var checkboxViewModel = new CheckboxItemViewModel(
                endDateCheckboxId,
                endDateCheckboxId,
                endDateCheckboxLabel,
                checkboxValue,
                endDateCheckboxHintText,
                null
                );

            var allStartDateErrors = (startDayErrors ?? new ModelErrorCollection())
                                     .Concat(startMonthErrors ?? new ModelErrorCollection())
                                     .Concat(startYearErrors ?? new ModelErrorCollection());
            var nonEmptyStartDateErrors = allStartDateErrors.Where(e => !string.IsNullOrWhiteSpace(e.ErrorMessage))
                                          .Select(e => e.ErrorMessage);

            var startDateModel = new DateInputViewModel(
                "start-date",
                "Start date",
                startDayId,
                startMonthId,
                startYearId,
                startDayValue,
                startMonthValue,
                startYearValue,
                startDayErrors?.Count > 0,
                startMonthErrors?.Count > 0,
                startYearErrors?.Count > 0,
                nonEmptyStartDateErrors,
                "nhsuk-u-margin-bottom-3"
                );

            var allEndDateErrors = (endDayErrors ?? new ModelErrorCollection())
                                   .Concat(endMonthErrors ?? new ModelErrorCollection())
                                   .Concat(endYearErrors ?? new ModelErrorCollection());
            var nonEmptyEndDateErrors = allEndDateErrors.Where(e => !string.IsNullOrWhiteSpace(e.ErrorMessage))
                                        .Select(e => e.ErrorMessage);

            var endDateModel = new DateInputViewModel(
                "conditional-end-date",
                "End date",
                endDayId,
                endMonthId,
                endYearId,
                endDayValue,
                endMonthValue,
                endYearValue,
                endDayErrors?.Count > 0,
                endMonthErrors?.Count > 0,
                endYearErrors?.Count > 0,
                nonEmptyEndDateErrors,
                "nhsuk-checkboxes__conditional" + (!checkboxValue ? " nhsuk-checkboxes__conditional--hidden" : "")
                );

            var viewModel = new DateRangeInputViewModel(
                id,
                label,
                startDateModel,
                endDateModel,
                checkboxViewModel,
                string.IsNullOrEmpty(hintText) ? null : hintText
                );

            return(View(viewModel));
        }