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 htmlAttributes = null) { return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.TextBox.CreateControl(textBoxOptions, htmlAttributes).Render()); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, TextBoxOptions textBoxOptions, System.Collections.Generic.IDictionary htmlAttributes = null) { if (expression == null) { throw new ArgumentNullException("expression"); } ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(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(); } //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()); } } }