kreta/KretaWeb/Helpers/TextBoxPasswordExtensions.cs
2024-03-13 00:33:46 +01:00

77 lines
2.6 KiB
C#

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<string> KretaPassword(this HtmlHelper helper, string name, bool enabled = true, Dictionary<string, object> htmlAttributes = null)
{
var tb = helper.Kendo().TextBox()
//.Name(Guid.NewGuid().ToString())
.Name(name)
.Enable(enabled);
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("type", "password");
htmlAttributes.Add("name", name);
tb.HtmlAttributes(htmlAttributes);
return tb;
}
public static TextBoxBuilder<string> KretaPasswordFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, Dictionary<string, object> 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<string, object>();
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;
}
}
}