예제 #1
0
        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));
        }
예제 #2
0
        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));
        }