Beispiel #1
0
 public FluentTagBuilder(string tagName, TagRenderMode renderMode = TagRenderMode.Normal, FluentTagBuilder parent = null)
 {
     this.tagBuilder    = new TagBuilder(tagName);
     this.renderMode    = renderMode;
     this.parent        = parent;
     this.stringBuilder = new StringBuilder();
 }
Beispiel #2
0
        //TODO: Test
        public static MvcHtmlString Table <T>(this HtmlHelper html, IEnumerable <T> items, object htmlAttributes = null)
        {
            var builder = new FluentTagBuilder("table")
                          .MergeAttributes(htmlAttributes)
                          .StartTag("thead")
                          .StartTag("tr");

            var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                builder = builder.StartTag("th").SetInnerText(property.Name).EndTag();
            }

            builder
            .EndTag()     // </tr>
            .EndTag()     // </thead>
            .StartTag("tbody");

            foreach (var item in items)
            {
                builder = builder.StartTag("tr");
                foreach (var property in properties)
                {
                    string value = property.GetValue(item).ToString();
                    builder = builder.StartTag("td").SetInnerText(value).EndTag();
                }
                builder = builder.EndTag(); // </tr>
            }
            builder = builder.EndTag();     // </tbody>

            return(MvcHtmlString.Create(builder.ToString()));
        }
Beispiel #3
0
        public static MvcHtmlString HelpText(this HtmlHelper html, string helpText, object htmlAttributes = null)
        {
            var tagBuilder = new FluentTagBuilder("p")
                             .AddCssClass("help-block")
                             .MergeAttributes(htmlAttributes)
                             .SetInnerHtml(helpText);

            return(new MvcHtmlString(tagBuilder.ToString()));
        }
Beispiel #4
0
        public static MvcHtmlString HelpTextFor <TModel, TProperty>(this HtmlHelper <TModel> html, Expression <Func <TModel, TProperty> > expression, object htmlAttributes = null)
        {
            var memberExpression = expression.Body as MemberExpression;
            var propertyInfo     = (memberExpression.Member as PropertyInfo);
            var attribute        = propertyInfo.GetCustomAttributes().OfType <LocalizedHelpTextAttribute>().FirstOrDefault();

            if (attribute == null)
            {
                return(MvcHtmlString.Empty);
            }

            var tagBuilder = new FluentTagBuilder("p")
                             .AddCssClass("help-block")
                             .MergeAttributes(htmlAttributes)
                             .SetInnerHtml(attribute.HelpText);

            return(new MvcHtmlString(tagBuilder.ToString()));
        }
Beispiel #5
0
        public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(
            this HtmlHelper <TModel> html,
            Expression <Func <TModel, IEnumerable <TProperty> > > expression,
            IEnumerable <SelectListItem> selectList,
            byte numberOfColumns          = 1,
            object labelHtmlAttributes    = null,
            object checkboxHtmlAttributes = null) where TModel : class
        {
            string htmlFieldName     = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
            string fullHtmlFieldId   = html.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName);

            var func           = expression.Compile();
            var selectedValues = func(html.ViewData.Model);

            var values = new List <string>();

            if (selectedValues != null)
            {
                values.AddRange(selectedValues.Select(x => Convert.ToString(x)));
            }

            if (selectList == null)
            {
                throw new ArgumentNullException("selectList");
            }

            int index = 0;

            var sb = new StringBuilder();

            bool groupByCategory = (selectList.First() is ExtendedSelectListItem);

            if (groupByCategory)
            {
                var items  = selectList.Cast <ExtendedSelectListItem>().ToList();
                var groups = items.GroupBy(x => x.Category);

                foreach (var @group in groups)
                {
                    sb.AppendFormat("<strong>{0}</strong>", group.Key);

                    foreach (var item in group)
                    {
                        var isChecked = values.Contains(item.Value);

                        var tagBuilder = new FluentTagBuilder("label")
                                         .MergeAttribute("for", fullHtmlFieldName)
                                         .MergeAttributes(labelHtmlAttributes)
                                         .StartTag("input", TagRenderMode.SelfClosing)
                                         .MergeAttribute("type", "checkbox")
                                         .MergeAttribute("name", fullHtmlFieldName)
                                         .MergeAttribute("id", fullHtmlFieldId + "_" + index)
                                         .MergeAttribute("value", item.Value);

                        if (isChecked)
                        {
                            tagBuilder = tagBuilder.MergeAttribute("checked", "checked");
                        }

                        if (checkboxHtmlAttributes != null)
                        {
                            tagBuilder = tagBuilder.MergeAttributes(checkboxHtmlAttributes);
                        }

                        tagBuilder = tagBuilder.EndTag(); //end checkbox
                        tagBuilder = tagBuilder
                                     .StartTag("span")
                                     .SetInnerText(item.Text)
                                     .EndTag();

                        sb.Append(tagBuilder.ToString());
                        index++;
                    }
                }
            }
            else
            {
                var rows        = (int)Math.Ceiling((selectList.Count() * 1d) / numberOfColumns);
                var columnWidth = (int)Math.Ceiling(12d / numberOfColumns);

                for (var i = 0; i < numberOfColumns; i++)
                {
                    var items = selectList.Skip(i * rows).Take(rows);
                    sb.AppendFormat("<div class=\"col-md-{0}\">", columnWidth);

                    foreach (var item in items)
                    {
                        var isChecked = values.Contains(item.Value);

                        var tagBuilder = new FluentTagBuilder("label")
                                         .MergeAttribute("for", fullHtmlFieldName)
                                         .MergeAttributes(labelHtmlAttributes)
                                         .StartTag("input", TagRenderMode.SelfClosing)
                                         .MergeAttribute("type", "checkbox")
                                         .MergeAttribute("name", fullHtmlFieldName)
                                         .MergeAttribute("id", fullHtmlFieldId + "_" + index)
                                         .MergeAttribute("value", item.Value);

                        if (isChecked)
                        {
                            tagBuilder = tagBuilder.MergeAttribute("checked", "checked");
                        }

                        if (checkboxHtmlAttributes != null)
                        {
                            tagBuilder = tagBuilder.MergeAttributes(checkboxHtmlAttributes);
                        }

                        tagBuilder = tagBuilder.EndTag(); //end checkbox
                        tagBuilder = tagBuilder
                                     .StartTag("span")
                                     .SetInnerText(item.Text)
                                     .EndTag();

                        sb.Append(tagBuilder.ToString());
                        index++;
                    }

                    sb.Append("</div>");
                }
            }

            return(new MvcHtmlString(sb.ToString()));
        }