37 lines
1.6 KiB
C#
37 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Mvc;
|
|
using Kendo.Mvc.UI;
|
|
|
|
namespace Kreta.Web.Helpers
|
|
{
|
|
public static class AutoCompleteExtensions
|
|
{
|
|
public static Kendo.Mvc.UI.Fluent.AutoCompleteBuilder KretaAutoCompleteFor<TModel, TProperty>(this HtmlHelper<TModel> helper, System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression, string id = "", string className = "")
|
|
{
|
|
var inputName = ExpressionHelper.GetExpressionText(expression);
|
|
var name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(inputName);
|
|
var val = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
|
|
|
|
if (helper.ViewData.ModelState[name] != null && helper.ViewData.ModelState[name].Errors.Count > 0)
|
|
className = string.Format("{0} input-validation-error", className);
|
|
|
|
var autocomplete = helper.Kendo().AutoCompleteFor(expression)
|
|
.Filter(FilterType.Contains)
|
|
.HighlightFirst(true).DataTextField("Text");
|
|
|
|
if (!string.IsNullOrWhiteSpace(className) || !string.IsNullOrWhiteSpace(id))
|
|
{
|
|
var attributes = new Dictionary<string, object>();
|
|
if (!string.IsNullOrWhiteSpace(className))
|
|
attributes.Add("class", className.Trim());
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
attributes.Add("id", id);
|
|
//attributes.Add("style", "height:17px");
|
|
autocomplete.HtmlAttributes(attributes);
|
|
}
|
|
|
|
return autocomplete;
|
|
}
|
|
}
|
|
}
|