using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; using Kendo.Mvc.UI; using Kendo.Mvc.UI.Fluent; namespace Kreta.Web.Helpers { public static class TextBoxPasswordExtensions { public static TextBoxBuilder KretaPassword(this HtmlHelper helper, string name, bool enabled = true, Dictionary htmlAttributes = null) { var tb = helper.Kendo().TextBox() //.Name(Guid.NewGuid().ToString()) .Name(name) .Enable(enabled); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } htmlAttributes.Add("type", "password"); htmlAttributes.Add("name", name); tb.HtmlAttributes(htmlAttributes); return tb; } public static TextBoxBuilder KretaPasswordFor(this HtmlHelper helper, Expression> expression, Dictionary htmlAttributes = null) { var fieldName = ExpressionHelper.GetExpressionText(expression); var fullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName); var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); var value = metadata.Model; if (htmlAttributes == null) htmlAttributes = new Dictionary(); var attributes = Mapper.GetUnobtrusiveValidationAttributes(helper, expression, htmlAttributes, metadata); foreach (var key in attributes.Keys) { try { if (!key.Contains("equalto") && htmlAttributes.ContainsKey(key) == false) { htmlAttributes.Add(key, attributes[key].ToString()); } } catch { } } var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata); if (metadata.IsRequired) { htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("data-labelmsg", metadata.DisplayName); } htmlAttributes.Add("type", "password"); var tb = helper.Kendo().TextBox() .Name(fullBindingName) .HtmlAttributes(htmlAttributes); if (value != null) tb.Value(value.ToString()); return tb; } } }