using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Web.Mvc; using System.Web.UI.WebControls; using System.Web.Mvc.Html; using System.Linq.Expressions; using System.Web.Routing; using Sleis.Models; namespace Sleis.HtmlHelpers { public static class AutoCompleteHelper { public static MvcHtmlString Autocomplete(this HtmlHelper htmlHelper, string name, object value, bool selectOnly, string targetName, string type, int typeYear, int size, int minLength, object htmlAttributes) { MvcHtmlString textBox = InputExtensions.TextBox(htmlHelper, name + ".AutoComplete", value, SetAutoCompleteAttributes(selectOnly, targetName, type, typeYear, size, minLength,htmlAttributes)); MvcHtmlString hiddenField = InputExtensions.Hidden(htmlHelper, name, value, new { @class = targetName }); return RenderMultipleControls(textBox, hiddenField); } public static MvcHtmlString AutocompleteFor(this HtmlHelper htmlHelper, Expression> expression, bool selectOnly, string targetName, string type, int typeYear, int size, int minLength, object htmlAttributes) { string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); object value = metaData.Model; MvcHtmlString textBox = InputExtensions.TextBox(htmlHelper, fullHtmlFieldName + ".AutoComplete", value, SetAutoCompleteAttributes(selectOnly, targetName, type, typeYear, size, minLength, htmlAttributes)); MvcHtmlString hiddenField = InputExtensions.HiddenFor(htmlHelper, expression, new{@class = targetName}); return RenderMultipleControls(textBox, hiddenField); } public static MvcHtmlString AutocompleteFor(this HtmlHelper htmlHelper, Expression> expression, object value, bool selectOnly, string targetName, string type, int typeYear, int size, int minLength, object htmlAttributes) { string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); MvcHtmlString textBox = InputExtensions.TextBox(htmlHelper, fullHtmlFieldName + ".AutoComplete", value, SetAutoCompleteAttributes(selectOnly, targetName, type, typeYear, size, minLength, htmlAttributes)); MvcHtmlString hiddenField = InputExtensions.HiddenFor(htmlHelper, expression, new { @class = targetName }); return RenderMultipleControls(textBox, hiddenField); } private static IDictionary SetAutoCompleteAttributes(bool selectOnly, string targetName, string type, int typeYear, int size, int minLength, object htmlAttributes) { IDictionaryattributes = new RouteValueDictionary(htmlAttributes); attributes["class"] += " autocomplete"; //append autocomplete to class attribute attributes.Add("selectOnly", selectOnly); attributes.Add("data-autocomplete-target", targetName); attributes.Add("data-autocomplete-type-year", typeYear); attributes.Add("data-autocomplete-size", size); attributes.Add("data-autocomplete-min-len", minLength ); return attributes; } private static MvcHtmlString RenderMultipleControls(params MvcHtmlString[] controls) { StringBuilder sb = new StringBuilder(); foreach(MvcHtmlString c in controls) { sb.Append(c.ToString()); } return MvcHtmlString.Create(sb.ToString()); } } }