コード例 #1
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();
        }
コード例 #2
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>
        }