public virtual IEnumerable<NestedTagBuilder> GenerateForEnum(ITagBuilderContext context)
        {
            Setup(context);

            var tags = new List<NestedTagBuilder>();
            foreach (var enumName in Enum.GetNames(Context.Metadata.ModelType))
            {
                var tagBuilder = CreatePrimaryTag("input");
                tagBuilder.MergeAttribute("type", "checkbox", true);
                if (!string.IsNullOrEmpty(Context.Metadata.Description))
                    tagBuilder.MergeAttribute("title", Context.Metadata.Description);


                if (GetValue() == enumName)
                    tagBuilder.MergeAttribute("checked", "checked");


                var label = new NestedTagBuilder("label");
                var title = new NestedTagBuilder("span");
                title.SetInnerText(LocalizedStringProvider.GetEnumString(Context.Metadata.ModelType, enumName));
                label.AddChild(tagBuilder);
                label.AddChild(title);

                tags.Add(label);
            }

            return tags;
        }
        public virtual IEnumerable<NestedTagBuilder> GenerateForEnum(ITagBuilderContext context)
        {
            Setup(context);

            var selectTag = CreatePrimaryTag("select");
            if (!string.IsNullOrEmpty(Context.Metadata.Description))
                selectTag.MergeAttribute("title", Context.Metadata.Description);

            var names = Enum.GetNames(getNonNullableModelType());
            if(context.Metadata.IsNullableValueType)
                names = new string[] { string.Empty }.Concat(names).ToArray();

            foreach (var enumName in names)
            {
                var tagBuilder = new NestedTagBuilder("option");
                tagBuilder.MergeAttribute("value", enumName, true);
                if (GetValue() == enumName)
                    tagBuilder.MergeAttribute("selected", "selected");

                if (enumName != string.Empty)
                {
                    var title = LocalizedStringProvider.GetEnumString(getNonNullableModelType(), enumName) ?? DefaultUICulture.FormatUnknown(enumName);
                    tagBuilder.SetInnerText(title);
                }

                selectTag.AddChild(tagBuilder);
            }

            return new[] {selectTag};
        }
        /// <summary>
        /// Generate options
        /// </summary>
        /// <param name="items">Items used to generate options</param>
        /// <param name="selectedValue">Selected value</param>
        /// <param name="formatter">Formatter used to find label/value. May be null if the list contains <c>SelectListItem</c></param>
        /// <returns>Generated <c>option</c> tags</returns>
        protected IEnumerable<NestedTagBuilder> GenerateOptions(IEnumerable items, string selectedValue,
                                                                ISelectItemFormatter formatter)
        {
            if (formatter == null)
            {
                if (!(items is IEnumerable<SelectListItem>))
                    throw new InvalidOperationException("No formatter was specified and the list do not contain SelectListItems");

                return GenerateOptions((IEnumerable<SelectListItem>)items, selectedValue);
            }

            var listItems = new List<NestedTagBuilder>();
            foreach (var item in items)
            {
                var tag = new NestedTagBuilder("option");
                var listItem = formatter.Generate(item);
                tag.MergeAttribute("value", listItem.Value);
                if (listItem.Value == selectedValue || listItem.Selected)
                    tag.MergeAttribute("selected", "selected");
                tag.SetInnerText(listItem.Text);
                listItems.Add(tag);
            }

            return listItems;
        }
Example #4
0
 /// <summary>
 /// Generates the primary/root tag (which will contain child tags if required)
 /// </summary>
 /// <param name="tagName">Name of the tag.</param>
 /// <returns>Tag builder for the tag</returns>
 /// <remarks>Adds all HTML attributes which has been specified in the Context, the name and the ID. Will
 /// also add any validation state members.</remarks>
 protected NestedTagBuilder CreatePrimaryTag(string tagName)
 {
     var tagBuilder = new NestedTagBuilder(tagName);
     tagBuilder.MergeAttributes(Context.HtmlAttributes);
     tagBuilder.MergeAttribute("name", Context.FullName, true);
     tagBuilder.GenerateId(Context.FullName);
     SetValidationState(tagBuilder, Context.FullName);
     return tagBuilder;
 }
        protected IEnumerable<NestedTagBuilder>  GenerateOptions(IEnumerable items, string selectedValue)
        {
            var listItems = new List<NestedTagBuilder>();
            foreach (SelectListItem listItem in items)
            {
                var tag = new NestedTagBuilder("option");
                tag.MergeAttribute("value", listItem.Value);
                if (listItem.Value == selectedValue || listItem.Selected)
                    tag.MergeAttribute("selected", "selected");
                tag.SetInnerText(listItem.Text);
                listItems.Add(tag);
            }

            return listItems;
        }
        /// <summary>
        /// Generate options
        /// </summary>
        /// <param name="items"></param>
        /// <param name="selectedValue"></param>
        /// <param name="formatter"></param>
        /// <returns></returns>
        protected IEnumerable<NestedTagBuilder> GenerateOptions(IEnumerable items, string selectedValue, ISelectItemFormatter formatter)
        {
            if (formatter == null)
                return GenerateOptions(items, selectedValue);

            var listItems = new List<NestedTagBuilder>();
            foreach (var item in items)
            {
                var tag = new NestedTagBuilder("option");
                var listItem = formatter.Generate(item);
                tag.MergeAttribute("value", listItem.Value);
                if (listItem.Value == selectedValue || listItem.Selected)
                    tag.MergeAttribute("selected", "selected");
                tag.SetInnerText(listItem.Text);
                listItems.Add(tag);
            }

            return listItems;

        }
        private IEnumerable<NestedTagBuilder> GenerateForSingleBox()
        {
            var tags = new NestedTagBuilder[2];

            tags[0] = CreatePrimaryTag("input");
            tags[0].MergeAttribute("type", "checkbox");
            tags[0].MergeAttribute("value", GetValue());
            if (Context.Metadata.Model != null)
            {
                bool isChecked;
                if (Boolean.TryParse(Context.Metadata.Model.ToString(), out isChecked) && isChecked)
                {
                    tags[0].MergeAttribute("checked", "checked");
                }
            }

            // add hidden input
            tags[1] = CreatePrimaryTag("input");
            tags[1].MergeAttribute("type", "hidden");
            tags[1].MergeAttribute("value", "false");

            return tags;
        }
Example #8
0
        public virtual IEnumerable<NestedTagBuilder> GenerateForEnum(ITagBuilderContext context)
        {
            Setup(context);

            var selectTag = CreatePrimaryTag("select");
            if (!string.IsNullOrEmpty(Context.Metadata.Description))
                selectTag.MergeAttribute("title", Context.Metadata.Description);

            foreach (var enumName in Enum.GetNames(Context.Metadata.ModelType))
            {
                var tagBuilder = new NestedTagBuilder("option");
                tagBuilder.MergeAttribute("value", enumName, true);
                if (GetValue() == enumName)
                    tagBuilder.MergeAttribute("selected", "selected");

                long corporateID = CurrentParemeter.GetCurrentCorporateId();
                var title = LocalizedStringProvider.GetEnumString(corporateID,Context.Metadata.ModelType, enumName);
                tagBuilder.SetInnerText(title);

                selectTag.AddChild(tagBuilder);
            }

            return new[] {selectTag};
        }
        private void SetValidationState(NestedTagBuilder tagBuilder, string fullName)
        {
            ModelState modelState;
            if (_viewContext.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0)
            {
                tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }

        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FormItemAdapterContext"/> class.
 /// </summary>
 /// <param name="tagBuilder">Generated tag builder.</param>
 public HtmlTagAdapterContext(NestedTagBuilder tagBuilder)
 {
     TagBuilder = tagBuilder;
 }