private static IHtmlString BuildRadioButton(string name, object value, bool?isChecked, IDictionary <string, object> attributes)
        {
            var valueString = Convert.ToString(value);

            var builder = new TagBuilder("input");

            builder.MergeAttribute("type", "radio", true);
            builder.GenerateId(name);
            builder.MergeAttributes(attributes, true);

            builder.MergeAttribute("value", valueString, true);
            builder.MergeAttribute("name", name, true);

            //if (UnobtrusiveJavaScriptEnabled)
            //{
            //    // Add validation attributes
            //    var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
            //    builder.MergeAttributes(validationAttributes, replaceExisting: false);
            //}

            //var modelState = ModelState[name];
            //string modelValue = null;
            //if (modelState != null)
            //{
            //    modelValue = ConvertTo(modelState.Value, typeof(string)) as string;
            //    isChecked = isChecked ?? String.Equals(modelValue, valueString, StringComparison.OrdinalIgnoreCase);
            //}

            if (isChecked.HasValue)
            {
                // Overrides attribute values
                if (isChecked.Value)
                {
                    builder.MergeAttribute("checked", "checked", true);
                }
                else
                {
                    builder.Attributes.Remove("checked");
                }
            }

            //AddErrorClass(builder, name);

            return(builder.ToHtmlString(TagRenderMode.SelfClosing));
        }
Esempio n. 2
0
        private static IHtmlString BuildDropDownList(string name, string defaultOption, IEnumerable <SelectListItem> selectList,
                                                     object selectedValue, IDictionary <string, object> htmlAttributes)
        {
            //var modelState = ModelState[name];
            //if (modelState != null)
            {
                selectedValue = selectedValue; //?? ModelState[name].Value;
            }
            selectedValue = Convert.ToString(selectedValue);

            if (selectedValue != null)
            {
                var newSelectList = new List <SelectListItem>(from item in selectList
                                                              select new SelectListItem(item));
                var comparer     = StringComparer.InvariantCultureIgnoreCase;
                var selectedItem = newSelectList.FirstOrDefault(item => item.Selected || comparer.Equals(item.Value ?? item.Text, selectedValue));
                if (selectedItem != default(SelectListItem))
                {
                    selectedItem.Selected = true;
                    selectList            = newSelectList;
                }
            }

            var tagBuilder = new TagBuilder("select")
            {
                InnerHtml = BuildListOptions(selectList, defaultOption)
            };

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", name, true);
            tagBuilder.GenerateId(name);

            //if (UnobtrusiveJavaScriptEnabled)
            //{
            //    var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
            //    tagBuilder.MergeAttributes(validationAttributes, replaceExisting: false);
            //}

            // If there are any errors for a named field, we add the css attribute.
            //AddErrorClass(tagBuilder, name);

            return(tagBuilder.ToHtmlString(TagRenderMode.Normal));
        }
Esempio n. 3
0
        private static IHtmlString BuildInputField(string name, InputType type, object value, bool isExplicitValue,
                                                   IDictionary <string, object> attributes)
        {
            var tagBuilder = new TagBuilder("input");

            // Implicit parameters
            tagBuilder.MergeAttribute("type", GetInputTypeString(type));
            tagBuilder.GenerateId(name);

            // Overwrite implicit
            tagBuilder.MergeAttributes(attributes, true);

            //if (UnobtrusiveJavaScriptEnabled)
            //{
            //    // Add validation attributes
            //    var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
            //    tagBuilder.MergeAttributes(validationAttributes, replaceExisting: false);
            //}

            // Function arguments
            tagBuilder.MergeAttribute("name", name, true);
            //var modelState = ModelState[name];
            //if ((type != InputType.Password) && modelState != null)
            {
                // Don't use model values for passwords
                value = value; //?? modelState.Value ?? String.Empty;
            }

            if ((type != InputType.Password) || ((type == InputType.Password) && (value != null)))
            {
                // Review: Do we really need to be this pedantic about sticking to mvc?
                tagBuilder.MergeAttribute("value", Convert.ToString(value), isExplicitValue);
            }
            //AddErrorClass(tagBuilder, name);
            return(tagBuilder.ToHtmlString(TagRenderMode.SelfClosing));
        }
Esempio n. 4
0
        private static IHtmlString BuildListBox(string name, string defaultOption, IEnumerable <SelectListItem> selectList,
                                                object selectedValues, int?size, bool allowMultiple, IDictionary <string, object> htmlAttributes)
        {
            //var modelState = ModelState[name];
            //if (modelState != null)
            {
                selectedValues = selectedValues;// ?? ModelState[name].Value;
            }

            if (selectedValues != null)
            {
                IEnumerable values = (allowMultiple) ? ConvertTo(selectedValues) : new[] { selectedValues.ToString() };

                var newSelectList    = new List <SelectListItem>();
                var selectedValueSet = new HashSet <string>(from object value in values
                                                            select Convert.ToString(value, CultureInfo.CurrentCulture),
                                                            StringComparer.OrdinalIgnoreCase);

                bool previousSelected = false;
                foreach (var item in selectList)
                {
                    bool selected = false;
                    // If the user's specified allowed multiple to be false
                    // only pick up the first item that was selected.
                    if (allowMultiple || !previousSelected)
                    {
                        selected = item.Selected || selectedValueSet.Contains(item.Value ?? item.Text);
                    }
                    previousSelected = previousSelected | selected;

                    newSelectList.Add(new SelectListItem(item)
                    {
                        Selected = selected
                    });
                }
                selectList = newSelectList;
            }

            var tagBuilder = new TagBuilder("select")
            {
                InnerHtml = BuildListOptions(selectList, defaultOption)
            };

            //if (UnobtrusiveJavaScriptEnabled)
            //{
            //    // Add validation attributes
            //    var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
            //    tagBuilder.MergeAttributes(validationAttributes, replaceExisting: false);
            //}

            tagBuilder.GenerateId(name);
            tagBuilder.MergeAttributes(htmlAttributes);

            tagBuilder.MergeAttribute("name", name, true);
            if (size.HasValue)
            {
                tagBuilder.MergeAttribute("size", size.ToString(), true);
            }
            if (allowMultiple)
            {
                tagBuilder.MergeAttribute("multiple", "multiple");
            }
            else if (tagBuilder.Attributes.ContainsKey("multiple"))
            {
                tagBuilder.Attributes.Remove("multiple");
            }

            // If there are any errors for a named field, we add the css attribute.
            //AddErrorClass(tagBuilder, name);

            return(tagBuilder.ToHtmlString(TagRenderMode.Normal));
        }