Пример #1
0
        /// <summary>
        /// Create an Image with innerHtml, cssClasses, name, glyphicon, buttonType, htmlAttributes.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="innerHtml">Display name</param>
        /// <param name="cssClass">Class for CSS</param>
        /// <param name="name">Name and Id</param>
        /// <param name="glyphicon">Glyphicon Boostrap Class</param>
        /// <param name="buttonType">Button type</param>
        /// <param name="htmlAttributes">Html Attributes</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper,
                                           string innerHtml,
                                           string cssClass,
                                           string glyphicon,
                                           string name,
                                           HtmlButtonTypes buttonType = null,
                                           object htmlAttributes      = null
                                           )
        {
            TagBuilder tb = new TagBuilder("button");

            if (!string.IsNullOrWhiteSpace(cssClass))
            {
                if (!cssClass.Contains("btn-"))
                {
                    cssClass = "btn-primary " + cssClass;
                }
            }
            else
            {
                cssClass = "btn-primary";
            }

            cssClass = "btn " + cssClass;

            tb.AddCssClass(cssClass);

            HtmlExtentionsCommon.AddName(tb, name);

            tb.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

            if (!string.IsNullOrWhiteSpace(glyphicon))
            {
                TagBuilder span = new TagBuilder("span");
                span.AddCssClass(glyphicon);
                span.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { aria_hidden = "true" }));

                span.InnerHtml = " " + innerHtml;

                tb.InnerHtml = span.ToString() + " ";
            }
            else
            {
                tb.InnerHtml += innerHtml;
            }

            if (buttonType == null || string.IsNullOrWhiteSpace(buttonType.Value))
            {
                buttonType = HtmlButtonTypes.Submit;
            }

            tb.MergeAttribute("type", buttonType.Value);

            return(MvcHtmlString.Create(tb.ToString()));
        }
Пример #2
0
        /// <summary>
        /// Create an Image with src, altText, cssClass, name, htmlAttributes.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="src">Image locale in the file i/o. </param>
        /// <param name="altText"></param>
        /// <param name="cssClass">Class for CSS.</param>
        /// <param name="name"></param>
        /// <param name="htmlAttributes">Attributes for the HTML5.</param>
        /// <returns></returns>
        public static MvcHtmlString Image(this HtmlHelper htmlHelper,
                                          string src,
                                          string altText,
                                          string cssClass,
                                          string name,
                                          object htmlAttributes = null)
        {
            TagBuilder tb = new TagBuilder("img");

            HtmlExtentionsCommon.AddName(tb, name);

            tb.MergeAttribute("src", src);
            tb.MergeAttribute("alt", altText);

            tb.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

            return(MvcHtmlString.Create(tb.ToString(TagRenderMode.SelfClosing)));
        }
Пример #3
0
        /// <summary>
        /// Create a Table Layout.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlhelper"></param>
        /// <param name="viewModel">Model with List to Show and Properties to Show.</param>
        /// <param name="withEdit">A boolean type for create a Table with Edit button</param>
        /// <param name="withRemove">A boolean type for create a Table with Remove button</param>
        /// <param name="withDetail">A boolean type for create a Table with Detail button</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString TableListFor <TModel, TEntity>(this HtmlHelper <TModel> htmlhelper,
                                                                   ITableList <TEntity> viewModel,
                                                                   bool withEdit         = false,
                                                                   bool withRemove       = false,
                                                                   bool withDetail       = false,
                                                                   object htmlAttributes = null) where TEntity : class
        {
            TagBuilder table = new TagBuilder("table");

            table.AddCssClass("table table-bordered table-hover table-responsive");

            TagBuilder tr = new TagBuilder("tr");

            foreach (var propertyToShow in viewModel.PropertiesToShow)
            {
                TagBuilder th = new TagBuilder("th");

                th.MergeAttribute("style", "cursor: default; ");

                th.InnerHtml = propertyToShow.DisplayProperty != null ? propertyToShow.DisplayProperty : propertyToShow.PropertyName;

                tr.InnerHtml += th.ToString();
            }

            if (withEdit)
            {
                tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Editar", new { style = "width: 5%; cursor: default;" }).ToString();
            }

            if (withDetail)
            {
                tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Detalhes", new { style = "width: 5%; cursor: default" }).ToString();
            }

            if (withRemove)
            {
                tr.InnerHtml += HtmlExtentionsCommon.CreateTagBuilder("th", "Remover", new { style = "width: 5%; cursor: default" }).ToString();
            }

            TagBuilder tHead = HtmlExtentionsCommon.CreateTagBuilder("thead", tr.ToString());


            TagBuilder tbody = new TagBuilder("tbody");

            foreach (var item in viewModel.List)
            {
                TagBuilder trItem = new TagBuilder("tr");
                var        propertiesFromTheItem = item.GetType().GetProperties();

                foreach (var property in propertiesFromTheItem)
                {
                    PropertyToShow showThisProperty = HtmlExtentionsCommon.ShowThisProperty(property, viewModel.PropertiesToShow, item);


                    if (showThisProperty != null)
                    {
                        string display = showThisProperty.DisplayProperty != null ? showThisProperty.DisplayProperty : showThisProperty.PropertyName;

                        TagBuilder td = new TagBuilder("td");
                        td.InnerHtml = item.GetType().GetProperty(property.Name).GetValue(item).ToString();
                        td.MergeAttribute("style", "padding-top: 15px");
                        trItem.InnerHtml += td.ToString();
                    }
                }

                if (withEdit)
                {
                    var button = HtmlExtentionsCommon.CreateTagBuilder("td",
                                                                       ButtonHtmlExtention.Button(
                                                                           htmlhelper, string.Empty,
                                                                           "btn-info col-lg-12",
                                                                           "glyphicon glyphicon-edit",
                                                                           string.Empty,
                                                                           HtmlButtonTypes.Button,
                                                                           new { data_action = "editar", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Editar" }
                                                                           ).ToString());

                    trItem.InnerHtml += button.ToString();
                }

                if (withDetail)
                {
                    var button = HtmlExtentionsCommon.CreateTagBuilder("td",
                                                                       ButtonHtmlExtention.Button(
                                                                           htmlhelper, string.Empty,
                                                                           "btn-success col-lg-10 col-lg-offset-1",
                                                                           "glyphicon glyphicon-search",
                                                                           string.Empty,
                                                                           HtmlButtonTypes.Button,
                                                                           new { data_action = "editar", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Detalhe" }
                                                                           ).ToString());
                    trItem.InnerHtml += button.ToString();
                }

                if (withRemove)
                {
                    var button = HtmlExtentionsCommon.CreateTagBuilder("td",
                                                                       ButtonHtmlExtention.Button(
                                                                           htmlhelper, string.Empty,
                                                                           "btn-danger col-lg-10 col-lg-offset-1",
                                                                           "glyphicon glyphicon-trash",
                                                                           string.Empty,
                                                                           HtmlButtonTypes.Button,
                                                                           new { data_action = "remover", data_actionId = item.GetType().GetProperty("Id").GetValue(item).ToString(), title = "Remover" }
                                                                           ).ToString());
                    trItem.InnerHtml += button.ToString();
                }

                tbody.InnerHtml += trItem.ToString();
            }
            table.InnerHtml  = tHead.ToString();
            table.InnerHtml += tbody.ToString();

            if (htmlAttributes != null)
            {
                var anonymousObject = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

                if (anonymousObject.Select(x => x.Value.ToString().Contains("margin")).FirstOrDefault())
                {
                    table.MergeAttributes(anonymousObject);
                }
                else
                {
                    anonymousObject.Add("style", "margin: 10px 0px");
                }
            }
            else if (htmlAttributes != null)
            {
                table.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { style = "margin: 10px 0px; form-group" }));
            }

            return(MvcHtmlString.Create(table.ToString()));
        }