Razor视图引擎与My97DatePicker插件的结合
1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.CompilerServices;
4 using System.Linq.Expressions;
5
6 namespace System.Web.Mvc
7 {
8 /// <summary>
9 /// Extended the HtmlHelper for Calendar
10 /// </summary>
11 public static class CalendarExtensions
12 {
13 private static string defaultFormat = "yyyy-MM-dd";
14
15 /// <summary>
16 /// 使用特定的名称生成控件
17 /// </summary>
18 /// <param name="helper">HtmlHelper对象</param>
19 /// <param name="name">控件名称</param>
20 /// <returns>Html文本</returns>
21 public static MvcHtmlString Calendar(this HtmlHelper helper, string name)
22 {
23 return Calendar(helper, name, defaultFormat);
24 }
25
26 /// <summary>
27 /// 使用特定的名称生成控件
28 /// </summary>
29 /// <param name="helper">HtmlHelper对象</param>
30 /// <param name="name">控件名称</param>
31 /// <param name="format">显示格式</param>
32 /// <returns>Html文本</returns>
33 public static MvcHtmlString Calendar(this HtmlHelper helper, string name, string format)
34 {
35 return GenerateHtml(name, null, format);
36 }
37
38 /// <summary>
39 /// 使用特定的名称和初始值生成控件
40 /// </summary>
41 /// <param name="helper">HtmlHelper对象</param>
42 /// <param name="name">控件名称</param>
43 /// <param name="date">要显示的日期时间</param>
44 /// <returns>Html文本</returns>
45 public static MvcHtmlString Calendar(this HtmlHelper helper, string name, DateTime date)
46 {
47 return Calendar(helper, name, date, defaultFormat);
48 }
49
50 /// <summary>
51 /// 使用特定的名称和初始值生成控件
52 /// </summary>
53 /// <param name="helper">HtmlHelper对象</param>
54 /// <param name="name">控件名称</param>
55 /// <param name="date">要显示的日期时间</param>
56 /// <param name="format">显示格式</param>
57 /// <returns>Html文本</returns>
58 public static MvcHtmlString Calendar(this HtmlHelper helper, string name, DateTime date, string format)
59 {
60 return GenerateHtml(name, date, format);
61 }
62
63 /// <summary>
64 /// 通过lambda表达式生成控件
65 /// </summary>
66 /// <param name="helper">HtmlHelper对象</param>
67 /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param>
68 /// <returns>Html文本</returns>
69 public static MvcHtmlString CalendarFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
70 {
71
72 return CalendarFor(helper, expression, defaultFormat);
73 }
74
75 /// <summary>
76 /// 通过lambda表达式生成控件
77 /// </summary>
78 /// <param name="helper">HtmlHelper对象</param>
79 /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param>
80 /// <param name="format">显示格式</param>
81 /// <returns>Html文本</returns>
82 public static MvcHtmlString CalendarFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string format)
83 {
84 string name = ExpressionHelper.GetExpressionText(expression);
85 DateTime value;
86
87 object data = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, helper.ViewData).Model;
88 if (data != null && DateTime.TryParse(data.ToString(), out value))
89 {
90 return GenerateHtml(name, value, format);
91 }
92 else
93 {
94 return GenerateHtml(name, null, format);
95 }
96 }
97
98 /// <summary>
99 /// 通过lambda表达式获取要显示的日期时间
100 /// </summary>
101 /// <param name="helper">HtmlHelper对象</param>
102 /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param>
103 /// <param name="format">显示格式</param>
104 /// <returns>Html文本</returns>
105 public static MvcHtmlString CalendarDisplayFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string format)
106 {
107 string name = ExpressionHelper.GetExpressionText(expression);
108 DateTime value;
109 string Htmlstr = "";
110 object data = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, helper.ViewData).Model;
111 if (data != null && DateTime.TryParse(data.ToString(), out value))
112 {
113 Htmlstr = value.ToString(format);
114 }
115 else
116 {
117 Htmlstr = string.Empty;
118 }
119 return MvcHtmlString.Create(Htmlstr);
120 }
121
122 /// <summary>
123 /// 通过lambda表达式获取要显示的日期时间
124 /// </summary>
125 /// <param name="helper">HtmlHelper对象</param>
126 /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param>
127 /// <returns>Html文本</returns>
128 public static MvcHtmlString CalendarDisplayFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
129 {
130 return CalendarDisplayFor(helper, expression, defaultFormat);
131 }
132
133 /// <summary>
134 /// 生成输入框的Html
135 /// </summary>
136 /// <param name="name">calendar的名称</param>
137 /// <param name="date">calendar的值</param>
138 /// <returns>html文本</returns>
139 private static MvcHtmlString GenerateHtml(string name, DateTime? date, string format)
140 {
141 string Htmlstr = "";
142 if (date != null)
143 {
144 Htmlstr = "<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\" onfocus=\"WdatePicker({dateFmt:'" + format + "'})\" class=\"Wdate\" value=\"" + date.Value.ToString(format) + "\" />";
145 }
146 else
147 {
148 Htmlstr = "<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\" onfocus=\"WdatePicker({dateFmt:'" + format + "'})\" class=\"Wdate\" value=\"\" />";
149 }
150 return MvcHtmlString.Create(Htmlstr);
151 }
152 }
153 }
使用方法
<td class="ms-ri">出生日期:</td>
<td style="vertical-align: middle">
@Html.CalendarFor(m => m.Birthday)
</td>