public static IHtmlContent Select2DropDownFor <TModel, TValue>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, List <SelectListItem> selectList, string defaultValue = null, object htmlAttribute = null, Select2Option option = null)
        {
            option ??= new Select2Option();
            var metadata      = html.GetModelExplorer(expression);
            var htmlFieldName = html.FieldNameFor(expression);
            var id            = html.FieldIdFor(expression);
            var displayName   = metadata.Metadata.DisplayName ?? metadata.Metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            var value         = metadata.Model;
            var items         = selectList.ToList();

            if (defaultValue.HasValue())
            {
                items.Insert(0, new SelectListItem {
                    Value = "", Text = defaultValue
                });
            }

            var attributes = ComponentUtility.MergeAttributes(htmlAttribute, new { @class = "form-control", placeholder = displayName, style = "width: 100%" });
            var editor     = html.DropDownListFor(expression, items, attributes);
            var result     =
                editor.ToHtmlString() +
                html.StyleOnce(ComponentUtility.GetCssTag(select2_css, select2_css_cdn)).ToHtmlString() +
                html.StyleOnce(ComponentUtility.GetCssTag(select2_custom_css, null)).ToHtmlString() +
                html.ScriptOnce(ComponentUtility.GetJsTag(select2_js, select2_js_cdn)).ToHtmlString() +
                (option.Attributes["language"] == null ? "" : html.ScriptOnce(ComponentUtility.GetJsTag(string.Format("DNZ.MvcComponents.Select2.i18n.{0}.js", option.Attributes["language"].ToString().Trim('\'')), null)).ToHtmlString()) +
                html.Script(@"
            <script>
                $(function(){
                    $(""#" + id + @""").select2(" + option.RenderOptions() + @").change(function () { 
                        $(this).valid();
                    });
                });
            </script>").ToHtmlString();

            return(new HtmlString(result));
        }
        public static IHtmlContent Select2DropDown <T1, T2>(this IHtmlHelper html, string name, T1 value, Dictionary <T1, T2> source, string defaultValue = null, object htmlAttribute = null, Select2Option option = null)
        {
            option ??= new Select2Option();
            var id         = html.GenerateIdFromName(name);
            var selectList = new SelectList(source, "Key", "Value", value);
            var items      = selectList.Cast <SelectListItem>().ToList();
            //if (defaultValue.HasValue())
            //    items.Insert(0, new SelectListItem { Value = null, Text = defaultValue });
            var attributes = ComponentUtility.MergeAttributes(htmlAttribute, new { @class = "form-control", style = "width: 100%" });
            var editor     = html.DropDownList(name, items, defaultValue, attributes);
            var result     =
                editor.ToHtmlString() +
                html.StyleOnce(ComponentUtility.GetCssTag(select2_css, select2_css_cdn)).ToHtmlString() +
                html.StyleOnce(ComponentUtility.GetCssTag(select2_custom_css, null)).ToHtmlString() +
                html.ScriptOnce(ComponentUtility.GetJsTag(select2_js, select2_js_cdn)).ToHtmlString() +
                (option.Attributes["language"] == null ? "" : html.ScriptOnce(ComponentUtility.GetJsTag(string.Format("DNZ.MvcComponents.Select2.i18n.{0}.js", option.Attributes["language"].ToString().Trim('\'')), null)).ToHtmlString()) +
                html.Script(@"
            <script>
                $(function(){
                    $(""#" + id + @""").select2(" + option.RenderOptions() + @").change(function () { 
                        $(this).valid();
                    });
                });
            </script>").ToHtmlString();

            return(new HtmlString(result));
        }
Esempio n. 3
0
        public static IHtmlContent BsSelect2DropDownFor <TModel, TValue>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, SelectList selectList, string icon = null, Select2Option option = null, ComponentDirection?dir = null, int lable_col = 2, int editor_col = 4)
        {
            string displayName = null;

            SetVariables(html, expression, ref displayName, out var style, out var dirName, out var label, out var validator, out _, lable_col, dir);
            option ??= new Select2Option();
            var editor = html.Select2DropDownFor(expression, selectList, option: option);
            var result = SetTemplate(label, icon, editor_col, validator, editor.ToHtmlString(), dirName);

            return(new HtmlString(result));
        }
Esempio n. 4
0
        public static IHtmlContent BsSelect2ListBoxFor <TModel, TValue, T1, T2>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Dictionary <T1, T2> source, string defaultValue = null, string icon = null, Select2Option option = null, ComponentDirection?dir = null, int lable_col = 2, int editor_col = 4)
        {
            string displayName = null;

            SetVariables(html, expression, ref displayName, out var style, out var dirName, out var label, out var validator, out var value, lable_col, dir);
            var selectList = new SelectList(source, "Key", "Value", value);
            var editor     = html.Select2ListBoxFor(expression, source, defaultValue, option);
            var result     = SetTemplate(label, icon, editor_col, validator, editor.ToHtmlString(), dirName);

            return(new HtmlString(result));
        }
Esempio n. 5
0
        public static IHtmlContent BsSelect2DropDownFor <TModel, TValue, T1, T2>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Dictionary <T1, T2> source, string defaultValue = null, string icon = null, Select2Option option = null, ComponentDirection?dir = null, int lable_col = 2, int editor_col = 4, object htmlAttribute = null, bool isLtr = false)
        {
            string displayName = null;

            SetVariables(html, expression, ref displayName, out var style, out var dirName, out var label, out var validator, out _, lable_col, dir);
            option ??= new Select2Option();
            var editor = html.Select2DropDownFor(expression, source, defaultValue, htmlAttribute, option);
            var result = SetTemplate(label, icon, editor_col, validator, editor.ToHtmlString(), dirName);

            return(new HtmlString(result));
        }