1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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<string, string> 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<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- AutoCompleteOptions autoCompleteOptions,
- System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
- {
- if (expression == null)
- {
- throw new ArgumentNullException("expression");
- }
- ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(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());
- }
- }
- }
|