TextBoxExtensions.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using Bowin.Web.Controls.Mvc;
  7. namespace System.Web.Mvc
  8. {
  9. public static class TextBoxExtensions
  10. {
  11. public static MvcHtmlString TextBox(this HtmlHelper htmlHelper,
  12. TextBoxOptions textBoxOptions,
  13. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  14. {
  15. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.TextBox.CreateControl(textBoxOptions, htmlAttributes).Render());
  16. }
  17. public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
  18. Expression<Func<TModel, TProperty>> expression,
  19. TextBoxOptions textBoxOptions,
  20. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  21. {
  22. if (expression == null)
  23. {
  24. throw new ArgumentNullException("expression");
  25. }
  26. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  27. //获取字段名称,对应标签的name
  28. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  29. textBoxOptions.ID = fullHtmlFieldName;
  30. textBoxOptions.Name = fullHtmlFieldName;
  31. textBoxOptions.Value = modelMetadata.Model;
  32. //TODO:针对于短日期调整 显示格式:2016-08
  33. if (textBoxOptions.TextBoxType == TextBoxType.Month && textBoxOptions.Value != null)
  34. {
  35. textBoxOptions.Value = Convert.ToDateTime(modelMetadata.Model).ToString("yyyy-MM");
  36. }
  37. if (textBoxOptions.TextBoxType == TextBoxType.yyyyMMdd && textBoxOptions.Value != null)
  38. {
  39. textBoxOptions.Value = Convert.ToDateTime(modelMetadata.Model).ToString("yyyyMMdd");
  40. }
  41. //MVC验证支持
  42. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  43. if (htmlAttributes == null)
  44. {
  45. htmlAttributes = new Dictionary<string, string>();
  46. }
  47. //if (textBoxOptions.TextBoxType == TextBoxType.Date || textBoxOptions.TextBoxType == TextBoxType.DateTime)
  48. //{
  49. // //由于日期时间控件无法兼容jquery.validation验证,所以必须改为使用easyui自带方式验证,暂时只支持必填验证
  50. // validateAttributes.ToList().ForEach(x => {
  51. // if (x.Key == "data-val-required")
  52. // {
  53. // textBoxOptions.IsRequired = true;
  54. // textBoxOptions.Validator.ValidType = "daterequired";
  55. // textBoxOptions.Validator.InvalidMessage = x.Value.ToString();
  56. // textBoxOptions.Validator.MissingMessage = x.Value.ToString();
  57. // }
  58. // });
  59. //}
  60. //else
  61. //{
  62. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  63. //}
  64. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.TextBox.CreateControl(textBoxOptions, htmlAttributes).Render());
  65. }
  66. }
  67. }