Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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();
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        //////////////////////////
        // 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);
        }
Esempio n. 12
0
        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>
        }
Esempio n. 13
0
        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();
        }
Esempio n. 14
0
 public static void LabelForModel(
     HtmlHelper html, XcstWriter output, string?labelText = null, HtmlAttribs?htmlAttributes = null) =>
 LabelHelper(html, output, html.ViewData.ModelMetadata, String.Empty, labelText, htmlAttributes);
Esempio n. 15
0
        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);
        }
Esempio n. 16
0
        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();
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        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);
Esempio n. 19
0
        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);
        }
Esempio n. 20
0
        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);
        }
Esempio n. 21
0
        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);
        }
Esempio n. 22
0
        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);
        }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
0
        // 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);
Esempio n. 26
0
        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);
        }
Esempio n. 27
0
        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);
        }
Esempio n. 28
0
 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);
Esempio n. 29
0
        //////////////////////////
        // 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);
Esempio n. 30
0
        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);
        }