예제 #1
0
        public override string ToHtmlString()
        {
            var tag = CreateTag();

            var    label = new HtmlTagBuilder("label");
            string id    = Context.Id;

            if (!string.IsNullOrWhiteSpace(id) && !Context.AllowMultiple)
            {
                label.Attributes.Add("for", id);
            }
            if (Context.IsHidden)
            {
                label.MergeAttribute("style", "display: none;", true);
            }
            if (Context.IsInvisible)
            {
                label.AddCssClass("invisible");
            }
            label.InnerHtml = tag.ToString() + Context.Header;

            string name = Context.Name;

            if (Context.IsInline)
            {
                if (!string.IsNullOrWhiteSpace(name))
                {
                    label.Attributes.Add("data-container-for", name);
                }
                label.AddCssClass("radio-inline");
                return(label.ToString());
            }

            string output = "<div class='" + (Context.AllowMultiple ? "checkbox" : "radio") + "'";

            if (!string.IsNullOrWhiteSpace(name))
            {
                output += " data-container-for='" + name + "'";
            }
            output += ">" + label.ToString() + "</div>";

            string description = Context.Description;

            if (!string.IsNullOrWhiteSpace(description))
            {
                output += "<div class='help-block'>" + description + "</div>";
            }
            return(output);
        }
예제 #2
0
        private static string GetSummary(HtmlHelper htmlHelper)
        {
            var viewContext = htmlHelper.ViewContext;

            System.Web.Mvc.FormContext formContext = viewContext.ClientValidationEnabled ? viewContext.FormContext : null;

            if (htmlHelper.ViewData.ModelState.IsValid && formContext == null)
            {
                return(null);
            }

            var           modelState  = GetModelStateList(htmlHelper);
            StringBuilder listBuilder = new StringBuilder();

            foreach (var state in modelState)
            {
                foreach (var error in state.Value.Errors)
                {
                    string message = error.ErrorMessage;
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        listBuilder.Append("<li>");

                        string key = state.Key;
                        if (!string.IsNullOrWhiteSpace(key) && !message.Contains(key))
                        {
                            int lastDot = key.LastIndexOf('.');
                            if (lastDot != -1)
                            {
                                string suffix = key.Substring(lastDot + 1);
                                if (message.Contains(suffix))
                                {
                                    key = key.Substring(0, lastDot);
                                }
                            }
                            listBuilder.Append("<strong>");
                            listBuilder.Append(key.Replace('.', ' ').ToWords(true));
                            listBuilder.Append("</strong> ");
                        }

                        listBuilder.Append(message);
                        listBuilder.AppendLine("</li>");
                    }
                }
            }
            if (listBuilder.Length == 0)
            {
                if (viewContext.UnobtrusiveJavaScriptEnabled || formContext == null)
                {
                    return(null);
                }

                listBuilder.AppendLine("<li style=\"display:none\"></li>");
            }

            var listTag = new HtmlTagBuilder("ul")
            {
                InnerHtml = listBuilder.ToString()
            };
            var divBuilder = new HtmlTagBuilder("div");

            divBuilder.AddCssClass(htmlHelper.ViewData.ModelState.IsValid ? HtmlHelper.ValidationSummaryValidCssClassName : HtmlHelper.ValidationSummaryCssClassName);
            divBuilder.AddCssClass("alert alert-danger fade in");
            if (formContext != null)
            {
                if (viewContext.UnobtrusiveJavaScriptEnabled)
                {
                    divBuilder.MergeAttribute("data-valmsg-summary", "true");
                }
                else
                {
                    divBuilder.GenerateId("validationSummary");
                    formContext.ValidationSummaryId      = divBuilder.Attributes["id"];
                    formContext.ReplaceValidationSummary = true;
                }
            }
            divBuilder.InnerHtml = "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>"
                                   + listTag.ToString();

            return(divBuilder.ToString());
        }
예제 #3
0
        public virtual bool CreateCell(StringBuilder cells, object cellData, string columnClassPrefix, bool isHeader, bool isTotal, GridColumnContext <TRow> context, object htmlAttributes)
        {
            var cell = new HtmlTagBuilder("div");

            int width = AddColClass(cell, columnClassPrefix, "xs", Position.Width(0), -1);

            width = AddColClass(cell, columnClassPrefix, "sm", Position.Width(1), width);
            width = AddColClass(cell, columnClassPrefix, "md", Position.Width(2), width);
            width = AddColClass(cell, columnClassPrefix, "lg", Position.Width(3), width);

            int offset = AddOffsetClass(cell, columnClassPrefix, "xs", Position.Offset(0), 0);

            offset = AddOffsetClass(cell, columnClassPrefix, "sm", Position.Offset(1), offset);
            offset = AddOffsetClass(cell, columnClassPrefix, "md", Position.Offset(2), offset);
            offset = AddOffsetClass(cell, columnClassPrefix, "lg", Position.Offset(3), offset);

            string style = Context.Style.ToString().ToLowerInvariant();

            if (Context.Style != GridColumnStyle.Text)
            {
                cell.AddCssClass(columnClassPrefix + "-" + style);
            }
            if (isHeader)
            {
                if (context.WrapHeader)
                {
                    cell.AddCssClass("wrap");
                }
                string valueId  = context.Index.ToString(CultureInfo.InvariantCulture);
                var    sortable = context.IsSortable;
                if (sortable != GridBooleanPlus.False)
                {
                    cell.MergeAttribute("data-sortable", valueId);
                    if (sortable != GridBooleanPlus.True)
                    {
                        cell.MergeAttribute("data-sortable-type", sortable.ToString().ToLowerInvariant());
                    }
                }
                var filterable = context.IsFilterable;
                if (filterable != GridBooleanPlus.False)
                {
                    cell.MergeAttribute("data-filterable", valueId);
                    if (filterable != GridBooleanPlus.True)
                    {
                        cell.MergeAttribute("data-filterable-type", filterable.ToString().ToLowerInvariant());
                    }
                }
                var groupable = context.IsGroupable;
                if (groupable != GridBooleanPlus.False)
                {
                    cell.MergeAttribute("data-groupable", valueId);
                    if (groupable != GridBooleanPlus.True)
                    {
                        cell.MergeAttribute("data-groupable-type", groupable.ToString().ToLowerInvariant());
                    }
                    var groupsExpanded = context.GroupsExpanded;
                    if (groupsExpanded != GridGroupsExpanded.First)
                    {
                        cell.MergeAttribute("data-groupable-expanded", groupsExpanded.ToString().ToLowerInvariant());
                    }
                }
            }
            else if (isTotal && context.Total > GridColumnTotal.Title) // Skip total titles
            {
                cell.MergeAttribute("data-total", context.Index.ToString(CultureInfo.InvariantCulture));
                cell.MergeAttribute("data-total-type", context.Total.ToString().ToLowerInvariant());
                switch (context.Subtotal)
                {
                case GridColumnSubtotal.Prefix:
                    if (context.Prefix != null)
                    {
                        cell.MergeAttribute("data-total-sub", "true");
                    }
                    break;

                case GridColumnSubtotal.Suffix:
                    if (context.Suffix != null)
                    {
                        cell.MergeAttribute("data-total-sub", "true");
                    }
                    break;
                }
                cell.MergeAttribute("data-total-style", context.Style.ToString().ToLowerInvariant());
            }
            bool hasData;

            if (cellData == null)
            {
                hasData = false;
            }
            else
            {
                string cellString = cellData.ToString();
                if (string.IsNullOrWhiteSpace(cellString))
                {
                    hasData = false;
                }
                else
                {
                    cell.InnerHtml = cellString;
                    hasData        = true;
                }
            }

            cell.MergeAttributes(htmlAttributes);
            AddCell(cells, cell);

            AddClearFix(cells, "visible-xs", Position.EndOfRow(0));
            AddClearFix(cells, "visible-sm", Position.EndOfRow(1));
            AddClearFix(cells, "visible-md", Position.EndOfRow(2));
            AddClearFix(cells, "visible-lg", Position.EndOfRow(3));

            return(hasData);
        }