public static void RadioButtonForModel( HtmlHelper htmlHelper, XcstWriter output, object value, bool isChecked, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = htmlHelper.ViewData.ModelMetadata; RadioButtonForMetadata(htmlHelper, output, metadata, String.Empty, value, /*isChecked: */ isChecked, htmlAttributes); }
public static void ValidationMessageFor <TModel, TProperty>( HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, string?validationMessage = null, HtmlAttribs?htmlAttributes = null, string?tag = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); ValidationMessageHelper(htmlHelper, output, metadata, expressionString, validationMessage, htmlAttributes, tag); }
static void InputImpl( HtmlHelper htmlHelper, XcstWriter output, string?type, ModelMetadata?metadata, string expression, object?value, bool?useViewData, string?format, HtmlAttribs?htmlAttributes) { InputType?inputType = GetInputType(type); bool checkBoxOrRadio = inputType == InputType.CheckBox || inputType == InputType.Radio; if (type != null && (inputType is null || checkBoxOrRadio) && (htmlAttributes is null || !htmlAttributes.ContainsKey("type"))) { htmlAttributes = htmlAttributes?.Clone() ?? new HtmlAttributeDictionary(); htmlAttributes["type"] = type; } if (checkBoxOrRadio) { // Don't want Input() to behave like RadioButton() or CheckBox() inputType = null; } if (inputType == InputType.Hidden) { #if ASPNETMVC if (value is Binary binaryValue) { value = binaryValue.ToArray(); } #endif if (value is byte[] byteArrayValue) { value = Convert.ToBase64String(byteArrayValue); } } if (useViewData is null) { useViewData = (value is null); } InputHelper( htmlHelper, output, inputType ?? InputType.Text, metadata: metadata, name: expression, value: value, useViewData: useViewData.Value, isChecked: false, setId: true, isExplicitValue: true, format: format, htmlAttributes: htmlAttributes); }
internal static void TextAreaHelper( HtmlHelper htmlHelper, XcstWriter output, ModelMetadata metadata, string name, IDictionary <string, object> rowsAndColumns, HtmlAttribs?htmlAttributes, string?innerHtmlPrefix = null) { string fullName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (String.IsNullOrEmpty(fullName)) { throw new ArgumentNullException(nameof(name)); } output.WriteStartElement("textarea"); HtmlAttributeHelper.WriteId(fullName, output); output.WriteAttributeString("name", fullName); HtmlAttributeHelper.WriteAttributes(rowsAndColumns, output); bool explicitRowsAndCols = rowsAndColumns != implicitRowsAndColumns; // If there are any errors for a named field, we add the css attribute. string?cssClass = (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out ModelState modelState) && modelState.Errors.Count > 0) ? HtmlHelper.ValidationInputCssClassName : null; HtmlAttributeHelper.WriteClass(cssClass, htmlAttributes, output); HtmlAttributeHelper.WriteAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata), output); // name cannnot be overridden, and class was already written // explicit rows and cols cannot be overridden HtmlAttributeHelper.WriteAttributes( htmlAttributes, output, excludeFn: n => n == "name" || n == "class" || (explicitRowsAndCols && (n == "rows" || n == "cols"))); string?value; if (modelState?.Value != null) { value = modelState.Value.AttemptedValue; } else if (metadata.Model != null) { value = Convert.ToString(metadata.Model, CultureInfo.CurrentCulture); } else { value = String.Empty; } // The first newline is always trimmed when a TextArea is rendered, so we add an extra one // in case the value being rendered is something like "\r\nHello". output.WriteString((innerHtmlPrefix ?? Environment.NewLine) + value); output.WriteEndElement(); }
static void InputForMetadata( HtmlHelper htmlHelper, XcstWriter output, string?type, ModelMetadata?metadata, string expression, object?value, string?format, HtmlAttribs?htmlAttributes) { if (value is null && metadata != null && GetInputType(type) != InputType.Password) { value = metadata.Model; } InputImpl(htmlHelper, output, type, metadata, expression, value, /*useViewData: */ false, format, htmlAttributes); }
static void RadioButtonForMetadata( HtmlHelper htmlHelper, XcstWriter output, ModelMetadata?metadata, string expression, object value, bool?isChecked, HtmlAttribs?htmlAttributes) { object?model = metadata?.Model; if (isChecked is null && model != null) { isChecked = RadioButtonValueEquals(value, model.ToString()); } RadioButtonHelper(htmlHelper, output, metadata, expression, value, isChecked, htmlAttributes); }
public static void CheckBoxFor <TModel>( HtmlHelper <TModel> htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, Expression <Func <TModel, bool> > expression, HtmlAttribs?htmlAttributes = null) { if (expression is null) { throw new ArgumentNullException(nameof(expression)); } ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); CheckBoxForMetadata(htmlHelper, package, output, metadata, expressionString, /*isChecked: */ null, htmlAttributes); }
public static void SelectFor <TModel, TProperty>( HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem>?selectList = null, bool multiple = false, HtmlAttribs?htmlAttributes = null) { if (expression is null) { throw new ArgumentNullException(nameof(expression)); } ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); SelectHelper(htmlHelper, output, metadata, expressionString, selectList, default(string), multiple, htmlAttributes); }
static void CheckBoxForMetadata( HtmlHelper htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, ModelMetadata?metadata, string expression, bool?isChecked, HtmlAttribs?htmlAttributes) { object?model = metadata?.Model; if (isChecked is null && model != null) { if (Boolean.TryParse(model.ToString(), out bool modelChecked)) { isChecked = modelChecked; } } CheckBoxHelper(htmlHelper, package, output, metadata, expression, isChecked, htmlAttributes); }
internal static void SelectHelper( HtmlHelper htmlHelper, XcstWriter output, ModelMetadata?metadata, string expression, IEnumerable <SelectListItem>?selectList, string?optionLabel, bool multiple, HtmlAttribs?htmlAttributes) { if (!multiple && optionLabel is null && selectList != null) { var optionList = selectList as OptionList; if (optionList?.AddBlankOption == true) { optionLabel = String.Empty; } } SelectInternal(htmlHelper, output, metadata, optionLabel, expression, selectList, multiple, htmlAttributes); }
////////////////////////// // RadioButton ////////////////////////// public static void RadioButton( HtmlHelper htmlHelper, XcstWriter output, string name, object value, HtmlAttribs?htmlAttributes = null) { if (value is null) { throw new ArgumentNullException(nameof(value)); } // checked attributes is implicit, so we need to ensure that the dictionary takes precedence. if (htmlAttributes?.ContainsKey("checked") == true) { RadioButtonHelper(htmlHelper, output, /*metadata: */ null, name, value, /*isChecked: */ null, htmlAttributes); return; } bool isChecked = RadioButtonValueEquals(value, htmlHelper.EvalString(name)); RadioButtonHelper(htmlHelper, output, /*metadata: */ null, name, value, isChecked, htmlAttributes); }
static void SelectInternal( HtmlHelper htmlHelper, XcstWriter output, ModelMetadata?metadata, string?optionLabel, string name, IEnumerable <SelectListItem>?selectList, bool allowMultiple, HtmlAttribs?htmlAttributes) { ViewDataDictionary viewData = htmlHelper.ViewData; string fullName = viewData.TemplateInfo.GetFullHtmlFieldName(name); if (String.IsNullOrEmpty(fullName)) { throw new ArgumentNullException(nameof(name)); } bool usedViewData = false; // If we got a null selectList, try to use ViewData to get the list of items. if (selectList is null) { selectList = GetSelectData(htmlHelper, name); usedViewData = true; } object?defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string)); // If we haven't already used ViewData to get the entire list of items then we need to // use the ViewData-supplied value before using the parameter-supplied value. if (defaultValue is null) { if (metadata is null) { if (!usedViewData && !String.IsNullOrEmpty(name)) { defaultValue = viewData.Eval(name); } } else { defaultValue = metadata.Model; } } if (defaultValue != null) { selectList = GetSelectListWithDefaultValue(selectList, defaultValue, allowMultiple); } output.WriteStartElement("select"); HtmlAttributeHelper.WriteId(fullName, output); output.WriteAttributeString("name", fullName); HtmlAttributeHelper.WriteBoolean("multiple", allowMultiple, output); // If there are any errors for a named field, we add the css attribute. string?cssClass = (viewData.ModelState.TryGetValue(fullName, out ModelState modelState) && modelState.Errors.Count > 0) ? HtmlHelper.ValidationInputCssClassName : null; var validationAttribs = htmlHelper.GetUnobtrusiveValidationAttributes( name, metadata #if !ASPNETMVC , excludeMinMaxLength: !allowMultiple #endif ); HtmlAttributeHelper.WriteClass(cssClass, htmlAttributes, output); HtmlAttributeHelper.WriteAttributes(validationAttribs, output); // name cannnot be overridden, and class was already written HtmlAttributeHelper.WriteAttributes(htmlAttributes, output, excludeFn: n => n == "name" || n == "class"); WriteOptions(optionLabel, selectList, output); output.WriteEndElement(); // </select> }
static void CheckBoxHelper( HtmlHelper htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, ModelMetadata?metadata, string name, bool?isChecked, HtmlAttribs?htmlAttributes) { XcstWriter inputWriter = DocumentWriter.CastElement(package, output); XcstWriter hiddenWriter = DocumentWriter.CastElement(package, output); bool explicitChecked = isChecked.HasValue; if (explicitChecked && htmlAttributes != null && htmlAttributes.ContainsKey("checked")) { htmlAttributes = htmlAttributes.Clone(); htmlAttributes.Remove("checked"); // Explicit value must override dictionary } InputHelper( htmlHelper, inputWriter, InputType.CheckBox, metadata, name, value: "true", useViewData: !explicitChecked, isChecked: isChecked ?? false, setId: true, isExplicitValue: false, format: null, htmlAttributes: htmlAttributes); string fullName = Name(htmlHelper, name); // Render an additional <input type="hidden".../> for checkboxes. This // addresses scenarios where unchecked checkboxes are not sent in the request. // Sending a hidden input makes it possible to know that the checkbox was present // on the page when the request was submitted. hiddenWriter.WriteStartElement("input"); hiddenWriter.WriteAttributeString("type", HtmlHelper.GetInputTypeString(InputType.Hidden)); hiddenWriter.WriteAttributeString("name", fullName); hiddenWriter.WriteAttributeString("value", "false"); hiddenWriter.WriteEndElement(); }
public static void LabelForModel( HtmlHelper html, XcstWriter output, string?labelText = null, HtmlAttribs?htmlAttributes = null) => LabelHelper(html, output, html.ViewData.ModelMetadata, String.Empty, labelText, htmlAttributes);
public static void CheckBoxForModel( HtmlHelper htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, bool isChecked, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = htmlHelper.ViewData.ModelMetadata; CheckBoxForMetadata(htmlHelper, package, output, metadata, /*expression: */ String.Empty, isChecked, htmlAttributes); }
internal static void LabelHelper( HtmlHelper html, XcstWriter output, ModelMetadata metadata, string expression, string?labelText = null, HtmlAttribs?htmlAttributes = null) { string htmlFieldName = expression; string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(resolvedLabelText)) { return; } string fullFieldName = html.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); output.WriteStartElement("label"); output.WriteAttributeString("for", TagBuilder.CreateSanitizedId(fullFieldName)); HtmlAttributeHelper.WriteAttributes(htmlAttributes, output); output.WriteString(resolvedLabelText); output.WriteEndElement(); }
public static void RadioButtonFor <TModel, TProperty>( HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, object value, HtmlAttribs?htmlAttributes = null) { if (value is null) { throw new ArgumentNullException(nameof(value)); } ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); RadioButtonForMetadata(htmlHelper, output, metadata, expressionString, value, /*isChecked: */ null, htmlAttributes); }
public static void SelectForModel( HtmlHelper htmlHelper, XcstWriter output, IEnumerable <SelectListItem>?selectList = null, bool multiple = false, HtmlAttribs?htmlAttributes = null) => SelectHelper(htmlHelper, output, htmlHelper.ViewData.ModelMetadata, String.Empty, selectList, default(string), multiple, htmlAttributes);
public static void TextArea( HtmlHelper htmlHelper, XcstWriter output, string name, object?value, int rows, int columns, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = ModelMetadata.FromStringExpression(name, htmlHelper.ViewData); if (value != null) { metadata.Model = value; } var rowsAndColumns = GetRowsAndColumnsDictionary(rows, columns); TextAreaHelper(htmlHelper, output, metadata, name, rowsAndColumns, htmlAttributes); }
public static void InputForModel( HtmlHelper htmlHelper, XcstWriter output, object?value = null, string?type = null, string?format = null, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = htmlHelper.ViewData.ModelMetadata; InputForMetadata(htmlHelper, output, type, metadata, /*expression: */ String.Empty, value, format, htmlAttributes); }
public static void InputFor <TModel, TProperty>( HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, string?type = null, string?format = null, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string exprString = ExpressionHelper.GetExpressionText(expression); InputForMetadata(htmlHelper, output, type, metadata, exprString, /*value: */ null, format, htmlAttributes); }
public static void Label( HtmlHelper html, XcstWriter output, string expression, string?labelText = null, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = ModelMetadata.FromStringExpression(expression, html.ViewData); LabelHelper(html, output, metadata, expression, labelText, htmlAttributes); }
static void RadioButtonHelper( HtmlHelper htmlHelper, XcstWriter output, ModelMetadata?metadata, string name, object value, bool?isChecked, HtmlAttribs?htmlAttributes) { bool explicitChecked = isChecked.HasValue; if (explicitChecked && htmlAttributes != null && htmlAttributes.ContainsKey("checked")) { htmlAttributes = htmlAttributes.Clone(); htmlAttributes.Remove("checked"); // Explicit value must override dictionary } InputHelper( htmlHelper, output, InputType.Radio, metadata, name, value, useViewData: false, isChecked: isChecked.GetValueOrDefault(), setId: true, isExplicitValue: true, format: null, htmlAttributes: htmlAttributes); }
public static void TextAreaFor <TModel, TProperty>( HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, HtmlAttribs?htmlAttributes = null) { if (expression is null) { throw new ArgumentNullException(nameof(expression)); } ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); TextAreaHelper(htmlHelper, output, metadata, expressionString, implicitRowsAndColumns, htmlAttributes); }
// Select public static void Select( HtmlHelper htmlHelper, XcstWriter output, string name, IEnumerable <SelectListItem>?selectList = null, bool multiple = false, HtmlAttribs?htmlAttributes = null) => SelectHelper(htmlHelper, output, default(ModelMetadata), name, selectList, default(string), multiple, htmlAttributes);
public static void TextArea( HtmlHelper htmlHelper, XcstWriter output, string name, object?value = null, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = ModelMetadata.FromStringExpression(name, htmlHelper.ViewData); if (value != null) { metadata.Model = value; } TextAreaHelper(htmlHelper, output, metadata, name, implicitRowsAndColumns, htmlAttributes); }
public static void LabelFor <TModel, TValue>( HtmlHelper <TModel> html, XcstWriter output, Expression <Func <TModel, TValue> > expression, string?labelText = null, HtmlAttribs?htmlAttributes = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string expressionString = ExpressionHelper.GetExpressionText(expression); LabelHelper(html, output, metadata, expressionString, labelText, htmlAttributes); }
public static void CheckBox( HtmlHelper htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, string name, bool isChecked, HtmlAttribs?htmlAttributes = null) => CheckBoxHelper(htmlHelper, package, output, default(ModelMetadata), name, isChecked, htmlAttributes);
////////////////////////// // Input ////////////////////////// public static void Input( HtmlHelper htmlHelper, XcstWriter output, string name, object?value = null, string?type = null, string?format = null, HtmlAttribs?htmlAttributes = null) => InputImpl(htmlHelper, output, type, /*metadata: */ null, name, value, /*useViewData: */ null, format, htmlAttributes);
public static void RadioButton( HtmlHelper htmlHelper, XcstWriter output, string name, object value, bool isChecked, HtmlAttribs?htmlAttributes = null) { if (value is null) { throw new ArgumentNullException(nameof(value)); } RadioButtonHelper(htmlHelper, output, /*metadata: */ null, name, value, isChecked, htmlAttributes); }