using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Linq.Expressions; namespace Bowin.Web.Controls.Mvc { public static class AutoCompleteExtensions { public static MvcHtmlString AutoComplete(this HtmlHelper htmlHelper, AutoCompleteOptions autoCompleteOptions, System.Collections.Generic.IDictionary htmlAttributes = null) { if (!string.IsNullOrEmpty(autoCompleteOptions.Url)) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); autoCompleteOptions.Url = urlHelper.Content(autoCompleteOptions.Url); } return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.AutoComplete.CreateControl(autoCompleteOptions, htmlAttributes).Render()); } public static MvcHtmlString AutoComplete(this HtmlHelper htmlHelper, Expression> expression, AutoCompleteOptions autoCompleteOptions, System.Collections.Generic.IDictionary htmlAttributes = null) { if (expression == null) { throw new ArgumentNullException("expression"); } ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); //获取值 //modelMetadata.Model //获取字段名称,对应标签的name string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); autoCompleteOptions.Name = fullHtmlFieldName; if (modelMetadata.Model != null) { autoCompleteOptions.DefaultValue = modelMetadata.Model; } if (!string.IsNullOrEmpty(autoCompleteOptions.Url)) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); autoCompleteOptions.Url = urlHelper.Content(autoCompleteOptions.Url); } return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.AutoComplete.CreateControl(autoCompleteOptions, htmlAttributes).Render()); } } }