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));
        }
        public static IHtmlContent Select2ListBoxFor <TModel, TValue, T1, T2>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Dictionary <T1, T2> source, 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;// == null ? null : Convert.ChangeType(metadata.Model, typeof(T1));
            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%", placeholder = displayName });
            var editor     = html.ListBoxFor(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() + @");
                });
            </script>").ToHtmlString();

            return(new HtmlString(result));
        }
Example #3
0
        public static IHtmlContent BsListBoxFor <TModel, TValue, T1, T2>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, Dictionary <T1, T2> source, string defaultValue = null, string icon = null, ComponentDirection?dir = null, int lable_col = 2, int editor_col = 4, object htmlAttribute = null)
        {
            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 items      = selectList.Cast <SelectListItem>().ToList();

            if (!string.IsNullOrEmpty(defaultValue))
            {
                items.Insert(0, new SelectListItem {
                    Value = null, Text = defaultValue
                });
            }

            var attributes = ComponentUtility.MergeAttributes(htmlAttribute, new { @class = "form-control", placeholder = displayName, dir = dirName, style });
            var editor     = html.ListBoxFor(expression, items, attributes);
            var result     = SetTemplate(label, icon, editor_col, validator, editor.ToHtmlString(), dirName);

            return(new HtmlString(result));
        }