public static IHtmlContent TagAutocompleteFor <TModel, TValue>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, IEnumerable <string> source, object htmlAttributes = null) { var option = new TagAutocompleteOption(); if (source != null) { option.Source(source); } return(html.TagAutocompleteFor(expression, option, htmlAttributes)); }
public static IHtmlContent TagAutocompleteFor <TModel, TValue>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, TagAutocompleteOption option, object htmlAttributes = null) { var metadata = html.GetModelExplorer(expression); var htmlFieldName = html.FieldNameFor(expression); var id = html.FieldIdFor(expression); var divId = id + "_autotag"; var value = metadata.Model ?? ""; var tag = new TagBuilder("div"); tag.AddCssClass("form-control"); tag.Attributes.Add("contenteditable", "true"); tag.Attributes.Add("id", id + "_autotag"); tag.MergeAttributes(new RouteValueDictionary(htmlAttributes)); tag.InnerHtml.SetContent(value.ToString()); var editor = html.HiddenFor(expression); html.ScriptOnce(ComponentUtility.GetJsTag(Bootstrap_Typeahead_js, null)); html.ScriptOnce(ComponentUtility.GetJsTag(Rangy_Core_js, null)); html.ScriptOnce(ComponentUtility.GetJsTag(Caret_Position_js, null)); html.ScriptOnce(ComponentUtility.GetJsTag(Bootstrap_Tagautocomplete_js, null)); html.Script(@" <script> $(function(){ $(""#" + divId + @""").tagautocomplete(" + option.RenderOptions() + @"); }); </script>"); return(new HtmlString(tag.ToHtmlString() + "\n" + editor.ToHtmlString())); }