public static MvcHtmlString Select2DropDownFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, SelectList selectList, string defaultValue = null, object htmlAttribute = null, Select2Option option = null) { option = option ?? new Select2Option(); var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var htmlFieldName = ExpressionHelper.GetExpressionText(expression); var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); var displayName = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); var value = metadata.Model; var items = selectList.Cast <SelectListItem>().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.StyleFileSingle(@"<link href=""" + ComponentUtility.GetWebResourceUrl(select2_css) + @""" rel=""stylesheet"" />").ToHtmlString() + html.StyleFileSingle(@"<link href=""" + ComponentUtility.GetWebResourceUrl(select2_custom_css) + @""" rel=""stylesheet"" />").ToHtmlString() + (option.Attributes["language"] == null ? "" : html.ScriptFileSingle(@"<script src=""" + ComponentUtility.GetWebResourceUrl(string.Format("Savosh.Component.Select2.i18n.{0}.js", option.Attributes["language"].ToString().Trim('\''))) + @"""></script>").ToHtmlString()) + html.ScriptFileSingle(@" <script src=""" + ComponentUtility.GetWebResourceUrl(select2_js) + @"""></script>").ToHtmlString() + html.Script(@" <script> $(function(){ $(""#" + id + @""").select2(" + option.RenderOptions() + @").change(function () { $(this).valid(); }); }); </script>").ToHtmlString(); return(MvcHtmlString.Create(result)); }
public static MvcHtmlString Select2DropDown <T1, T2>(this HtmlHelper html, string name, T1 value, Dictionary <T1, T2> source, string defaultValue = null, object htmlAttribute = null, Select2Option option = null) { option = option ?? new Select2Option(); var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(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.StyleFileSingle(@"<link href=""" + ComponentUtility.GetWebResourceUrl(select2_css) + @""" rel=""stylesheet"" />").ToHtmlString() + html.StyleFileSingle(@"<link href=""" + ComponentUtility.GetWebResourceUrl(select2_custom_css) + @""" rel=""stylesheet"" />").ToHtmlString() + (option.Attributes["language"] == null ? "" : html.ScriptFileSingle(@"<script src=""" + ComponentUtility.GetWebResourceUrl(string.Format("Savosh.Component.Select2.i18n.{0}.js", option.Attributes["language"].ToString().Trim('\''))) + @"""></script>").ToHtmlString()) + html.ScriptFileSingle(@" <script src=""" + ComponentUtility.GetWebResourceUrl(select2_js) + @"""></script>").ToHtmlString() + html.Script(@" <script> $(function(){ $(""#" + id + @""").select2(" + option.RenderOptions() + @").change(function () { $(this).valid(); }); }); </script>").ToHtmlString(); return(MvcHtmlString.Create(result)); }