internal static IHtmlContent GenerateHtml <TModel, TProperty>( IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > propertyLambdaExpression, LabelViewModel labelOptions = null, HintViewModel hintOptions = null, FormGroupViewModel formGroupOptions = null, string classes = null, TextInputAppendixViewModel textInputAppendix = null, string type = "text", string autocomplete = null ) where TModel : GovUkViewModel { PropertyInfo property = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression); string propertyName = property.Name; TModel model = htmlHelper.ViewData.Model; string currentValue = ExtensionHelpers.GetCurrentValue(model, property, propertyLambdaExpression); string id = $"GovUk_{propertyName}"; if (labelOptions != null) { labelOptions.For = id; } var textInputViewModel = new TextInputViewModel { Name = $"GovUk_Text_{propertyName}", Id = id, Value = currentValue, Label = labelOptions, Hint = hintOptions, FormGroup = formGroupOptions, Classes = classes, TextInputAppendix = textInputAppendix, Type = type, Attributes = new Dictionary <string, string> { { "autocomplete", autocomplete } } }; if (model.HasErrorFor(property)) { textInputViewModel.ErrorMessage = new ErrorMessageViewModel { Text = model.GetErrorFor(property) }; } return(htmlHelper.Partial("/GovUkDesignSystemComponents/TextInput.cshtml", textInputViewModel)); }
internal static IHtmlContent GenerateHtml <TModel, TProperty>( IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > propertyLambdaExpression, LabelViewModel labelOptions = null, HintViewModel hintOptions = null, FormGroupViewModel formGroupOptions = null, string classes = null, TextInputAppendixViewModel textInputAppendix = null ) where TModel : GovUkViewModel { var property = ExpressionHelpers.GetPropertyFromExpression(propertyLambdaExpression); var propertyName = property.Name; var model = htmlHelper.ViewData.Model; var id = $"GovUk_{propertyName}"; var currentValue = ExtensionHelpers.GetCurrentValue(model, property, propertyLambdaExpression); if (labelOptions != null) { labelOptions.For = id; } var textInputViewModel = new TextInputViewModel { Name = $"GovUk_Text_{propertyName}", Id = id, Value = currentValue, Label = labelOptions, Hint = hintOptions, FormGroup = formGroupOptions, Classes = classes, TextInputAppendix = textInputAppendix }; if (model.HasErrorFor(property)) { textInputViewModel.ErrorMessage = new ErrorMessageViewModel { Text = model.GetErrorFor(property) } } ; return(htmlHelper.Partial("~/Partials/TextInput.cshtml", textInputViewModel)); } }
public static IHtmlContent GovUkTextInputFor <TModel>( this IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, int?> > propertyLambdaExpression, LabelViewModel labelOptions = null, HintViewModel hintOptions = null, FormGroupViewModel formGroupOptions = null, string classes = null, TextInputAppendixViewModel textInputAppendix = null) where TModel : GovUkViewModel { return(TextInputHtmlGenerator.GenerateHtml( htmlHelper, propertyLambdaExpression, labelOptions, hintOptions, formGroupOptions, classes, textInputAppendix)); }