Example #1
0
        internal static void WriteOption(SelectListItem item, XcstWriter output)
        {
            output.WriteStartElement("option");

            if (item.Value != null)
            {
                output.WriteAttributeString("value", item.Value);
            }

            HtmlAttributeHelper.WriteBoolean("selected", item.Selected, output);
            HtmlAttributeHelper.WriteBoolean("disabled", item.Disabled, output);

            output.WriteString(item.Text);
            output.WriteEndElement();
        }
Example #2
0
        static void WriteOptions(string?optionLabel, IEnumerable <SelectListItem> selectList, XcstWriter output)
        {
            // Make optionLabel the first item that gets rendered.

            if (optionLabel != null)
            {
                WriteOption(new SelectListItem {
                    Text     = optionLabel,
                    Value    = String.Empty,
                    Selected = false
                }, output);
            }

            // Group items in the SelectList if requested.
            // Treat each item with Group == null as a member of a unique group
            // so they are added according to the original order.

            var groupedSelectList = selectList.GroupBy <SelectListItem, int>(
                i => (i.Group is null) ? i.GetHashCode() : i.Group.GetHashCode());

            foreach (var group in groupedSelectList)
            {
                SelectListGroup?optGroup = group.First().Group;

                if (optGroup != null)
                {
                    output.WriteStartElement("optgroup");

                    if (optGroup.Name != null)
                    {
                        output.WriteAttributeString("label", optGroup.Name);
                    }

                    HtmlAttributeHelper.WriteBoolean("disabled", optGroup.Disabled, output);
                }

                foreach (SelectListItem item in group)
                {
                    WriteOption(item, output);
                }

                if (optGroup != null)
                {
                    output.WriteEndElement(); // </optgroup>
                }
            }
        }
Example #3
0
        public static void BooleanTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            ViewDataDictionary viewData = html.ViewData;

            bool?value = null;

            if (viewData.Model != null)
            {
                value = Convert.ToBoolean(viewData.Model, CultureInfo.InvariantCulture);
            }

            if (viewData.ModelMetadata.IsNullableValueType)
            {
                output.WriteStartElement("select");

                string?className = DefaultEditorTemplates.GetEditorCssClass(new EditorInfo("Boolean", "select"), "list-box tri-state");

                HtmlAttributeHelper.WriteClass(className, null, output);
                HtmlAttributeHelper.WriteBoolean("disabled", true, output);

                foreach (SelectListItem item in DefaultEditorTemplates.TriStateValues(value))
                {
                    SelectInstructions.WriteOption(item, output);
                }

                output.WriteEndElement();
            }
            else
            {
                output.WriteStartElement("input");
                output.WriteAttributeString("type", "checkbox");

                string?className = DefaultEditorTemplates.GetEditorCssClass(new EditorInfo("Boolean", "input", InputType.CheckBox), "check-box");

                HtmlAttributeHelper.WriteClass(className, null, output);
                HtmlAttributeHelper.WriteBoolean("disabled", true, output);
                HtmlAttributeHelper.WriteBoolean("checked", value.GetValueOrDefault(), output);

                output.WriteEndElement();
            }
        }
Example #4
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>
        }