1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- using Bowin.Web.Controls.Mvc;
- namespace System.Web.Mvc
- {
- public static class TextBoxExtensions
- {
- public static MvcHtmlString TextBox(this HtmlHelper htmlHelper,
- TextBoxOptions textBoxOptions,
- System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
- {
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.TextBox.CreateControl(textBoxOptions, htmlAttributes).Render());
- }
- public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- TextBoxOptions textBoxOptions,
- System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
- {
- if (expression == null)
- {
- throw new ArgumentNullException("expression");
- }
- ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
- //获取字段名称,对应标签的name
- string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
- textBoxOptions.ID = fullHtmlFieldName;
- textBoxOptions.Name = fullHtmlFieldName;
- textBoxOptions.Value = modelMetadata.Model;
- //TODO:针对于短日期调整 显示格式:2016-08
- if (textBoxOptions.TextBoxType == TextBoxType.Month && textBoxOptions.Value != null)
- {
- textBoxOptions.Value = Convert.ToDateTime(modelMetadata.Model).ToString("yyyy-MM");
- }
- if (textBoxOptions.TextBoxType == TextBoxType.yyyyMMdd && textBoxOptions.Value != null)
- {
- textBoxOptions.Value = Convert.ToDateTime(modelMetadata.Model).ToString("yyyyMMdd");
- }
- //MVC验证支持
- var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
- if (htmlAttributes == null)
- {
- htmlAttributes = new Dictionary<string, string>();
- }
- //if (textBoxOptions.TextBoxType == TextBoxType.Date || textBoxOptions.TextBoxType == TextBoxType.DateTime)
- //{
- // //由于日期时间控件无法兼容jquery.validation验证,所以必须改为使用easyui自带方式验证,暂时只支持必填验证
- // validateAttributes.ToList().ForEach(x => {
- // if (x.Key == "data-val-required")
- // {
- // textBoxOptions.IsRequired = true;
- // textBoxOptions.Validator.ValidType = "daterequired";
- // textBoxOptions.Validator.InvalidMessage = x.Value.ToString();
- // textBoxOptions.Validator.MissingMessage = x.Value.ToString();
- // }
- // });
- //}
- //else
- //{
- validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
- //}
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.TextBox.CreateControl(textBoxOptions, htmlAttributes).Render());
- }
- }
- }
|