private static IHtmlString BuildTextArea(string name, string value, IDictionary <string, object> rowsAndColumnsDictionary, IDictionary <string, object> htmlAttributes)
        {
            var tagBuilder = new TagBuilder("textarea");

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

            // Add user specified htmlAttributes
            tagBuilder.MergeAttributes(htmlAttributes);

            tagBuilder.MergeAttributes(rowsAndColumnsDictionary, rowsAndColumnsDictionary != ImplicitRowsAndColumns);

            // Value becomes the inner html of the textarea element
            //var modelState = ModelState[name];
            //if (modelState != null)
            {
                value = value; //?? Convert.ToString(ModelState[name].Value, CultureInfo.CurrentCulture);
            }
            tagBuilder.InnerHtml = String.IsNullOrEmpty(value) ? String.Empty : HttpUtility.HtmlEncode(value);

            //Assign name and id
            tagBuilder.MergeAttribute("name", name);
            tagBuilder.GenerateId(name);

            //AddErrorClass(tagBuilder, name);

            return(tagBuilder.ToHtmlString(TagRenderMode.Normal));
        }
        private static IHtmlString BuildCheckBox(string name, bool?isChecked, IDictionary <string, object> attributes)
        {
            var builder = new TagBuilder("input");

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

            //var model = ModelState[name];
            //if (model != null && model.Value != null)
            {
                //    var modelValue = Convert.ToBoolean(model.Value);
                isChecked = isChecked; //?? modelValue;
            }
            if (isChecked.HasValue)
            {
                if (isChecked.Value == true)
                {
                    builder.MergeAttribute("checked", "checked", true);
                }
                else
                {
                    builder.Attributes.Remove("checked");
                }
            }

            return(new NonEncodedHtmlString((builder.ToHtmlString(TagRenderMode.SelfClosing).ToHtmlString())));
        }
        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));
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public static IHtmlString Label <TModel>(this HtmlHelpers <TModel> helper, string labelText, string labelFor, IDictionary <string, object> attributes)
        {
            if (String.IsNullOrEmpty(labelText))
            {
                throw new ArgumentException("Argument_Cannot_Be_Null_Or_Empty", "labelText");
            }

            labelFor = labelFor ?? labelText;

            var tag = new TagBuilder("label")
            {
                InnerHtml = String.IsNullOrEmpty(labelText) ? String.Empty : HttpUtility.HtmlEncode(labelText)
            };

            if (!String.IsNullOrEmpty(labelFor))
            {
                tag.MergeAttribute("for", labelFor);
            }
            tag.MergeAttributes(attributes, false);

            return(tag.ToHtmlString(TagRenderMode.Normal));
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
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));
        }