private static MvcHtmlString InternalEntityListDetail <T>(this HtmlHelper helper, EntityListDetail listDetail)
        {
            if (!listDetail.Visible || listDetail.HideIfNull && listDetail.UntypedValue == null)
            {
                return(MvcHtmlString.Empty);
            }

            HtmlStringBuilder sb = new HtmlStringBuilder();

            using (sb.SurroundLine(new HtmlTag("div", listDetail.Prefix).Class("SF-entity-list-detail SF-control-container")))
            {
                sb.AddLine(helper.Hidden(listDetail.Compose(EntityListBaseKeys.ListPresent), ""));

                using (sb.SurroundLine(new HtmlTag("div", listDetail.Compose("hidden")).Class("hide")))
                {
                }

                HtmlStringBuilder sbSelect = new HtmlStringBuilder();

                var sbSelectContainer = new HtmlTag("select").Attr("size", "6").Class("form-control")
                                        .IdName(listDetail.Compose(EntityListBaseKeys.List));

                if (listDetail.ListHtmlProps.Any())
                {
                    sbSelectContainer.Attrs(listDetail.ListHtmlProps);
                }

                using (sbSelect.SurroundLine(sbSelectContainer))
                {
                    if (listDetail.UntypedValue != null)
                    {
                        foreach (var itemTC in TypeContextUtilities.TypeElementContext((TypeContext <MList <T> >)listDetail.Parent))
                        {
                            sb.Add(InternalListDetailElement(helper, sbSelect, itemTC, listDetail));
                        }
                    }
                }

                using (sb.SurroundLine(new HtmlTag("div", listDetail.Compose("inputGroup")).Class("input-group")))
                {
                    sb.Add(sbSelect.ToHtml());

                    using (sb.SurroundLine(new HtmlTag("span", listDetail.Compose("shownButton")).Class("input-group-btn btn-group-vertical")))
                    {
                        sb.AddLine(EntityButtonHelper.Create(helper, listDetail, btn: true));
                        sb.AddLine(EntityButtonHelper.Find(helper, listDetail, btn: true));
                        sb.AddLine(EntityButtonHelper.Remove(helper, listDetail, btn: true));
                        sb.AddLine(EntityButtonHelper.MoveUp(helper, listDetail, btn: true));
                        sb.AddLine(EntityButtonHelper.MoveDown(helper, listDetail, btn: true));
                    }
                }

                if (listDetail.ElementType.IsEmbeddedEntity() && listDetail.Create)
                {
                    T embedded = (T)(object)new ConstructorContext(helper.ViewContext.Controller).ConstructUntyped(typeof(T));
                    TypeElementContext <T> templateTC = new TypeElementContext <T>(embedded, (TypeContext)listDetail.Parent, 0, null);
                    sb.AddLine(EntityBaseHelper.EmbeddedTemplate(listDetail, EntityBaseHelper.RenderContent(helper, templateTC, RenderContentMode.Content, listDetail), templateTC.Value.ToString()));
                }

                sb.AddLine(listDetail.ConstructorScript(JsModule.Lines, "EntityListDetail"));
            }

            var formGroup = helper.FormGroup(listDetail, listDetail.Prefix, listDetail.LabelHtml ?? listDetail.LabelText.FormatHtml(), sb.ToHtml());

            if (listDetail.DetailDiv != listDetail.Compose(EntityBaseKeys.Detail))
            {
                return(formGroup);
            }

            HtmlStringBuilder sb2 = new HtmlStringBuilder();

            sb2.Add(formGroup);
            using (sb2.SurroundLine(new HtmlTag("fieldset")))
                sb2.AddLine(helper.Div(listDetail.DetailDiv, null, "SF-entity-list-detail-detaildiv"));

            return(sb2.ToHtml());
        }
        private static MvcHtmlString InternalEntityList <T>(this HtmlHelper helper, EntityList entityList)
        {
            if (!entityList.Visible || entityList.HideIfNull && entityList.UntypedValue == null)
            {
                return(MvcHtmlString.Empty);
            }

            HtmlStringBuilder sb = new HtmlStringBuilder();

            using (sb.SurroundLine(new HtmlTag("div", entityList.Prefix).Class("SF-entity-list SF-control-container")))
            {
                sb.AddLine(helper.Hidden(entityList.Compose(EntityListBaseKeys.ListPresent), ""));

                using (sb.SurroundLine(new HtmlTag("div", entityList.Compose("hidden")).Class("hide")))
                {
                }

                HtmlStringBuilder sbSelect = new HtmlStringBuilder();

                var sbSelectContainer = new HtmlTag("select").Attr("size", "6").Class("form-control")
                                        .IdName(entityList.Compose(EntityListBaseKeys.List));

                if (entityList.ListHtmlProps.Any())
                {
                    sbSelectContainer.Attrs(entityList.ListHtmlProps);
                }

                using (sbSelect.SurroundLine(sbSelectContainer))
                {
                    if (entityList.UntypedValue != null)
                    {
                        foreach (var itemTC in TypeContextUtilities.TypeElementContext((TypeContext <MList <T> >)entityList.Parent))
                        {
                            sb.Add(InternalListElement(helper, sbSelect, itemTC, entityList));
                        }
                    }
                }

                using (sb.SurroundLine(new HtmlTag("div", entityList.Compose("inputGroup")).Class("input-group")))
                {
                    sb.Add(sbSelect.ToHtml());

                    using (sb.SurroundLine(new HtmlTag("span", entityList.Compose("shownButton")).Class("input-group-btn btn-group-vertical")))
                    {
                        sb.AddLine(EntityButtonHelper.Create(helper, entityList, btn: true));
                        sb.AddLine(EntityButtonHelper.Find(helper, entityList, btn: true));
                        sb.AddLine(EntityButtonHelper.View(helper, entityList, btn: true));
                        sb.AddLine(EntityButtonHelper.Remove(helper, entityList, btn: true));
                        sb.AddLine(EntityButtonHelper.MoveUp(helper, entityList, btn: true));
                        sb.AddLine(EntityButtonHelper.MoveDown(helper, entityList, btn: true));
                    }
                }

                if (entityList.ElementType.IsEmbeddedEntity() && entityList.Create)
                {
                    T embedded = (T)(object)new ConstructorContext(helper.ViewContext.Controller).ConstructUntyped(typeof(T));
                    TypeElementContext <T> templateTC = new TypeElementContext <T>(embedded, (TypeContext)entityList.Parent, 0, null);
                    sb.AddLine(EntityBaseHelper.EmbeddedTemplate(entityList, EntityBaseHelper.RenderPopup(helper, templateTC, RenderPopupMode.Popup, entityList, isTemplate: true), null));
                }
                sb.AddLine(entityList.ConstructorScript(JsModule.Lines, "EntityList"));
            }

            return(helper.FormGroup(entityList, entityList.Prefix, entityList.LabelHtml ?? entityList.LabelText.FormatHtml(), sb.ToHtml()));
        }