在MVC的视图中可以直接写Html,也可以使用HtmlHelper来创建。这篇文章介绍创建一个HtmlHelper扩展,用来提供日期时间选择输入。
这里我们扩展HtmlHelper,就像它包含在ASP.NET MVC中一样,扩展方法使我们能为已有的类添加方法。这里使用了一个日期时间选择控件:My97DatePicker,需要添加到网站中,并在页面中引用。
先看看是怎么扩展的:
using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Linq.Expressions; namespace System.Web.Mvc { ??? /// <summary> ??? /// Extended the HtmlHelper for Calendar ??? /// </summary> ??? public static class CalendarExtensions ??? { ??????? private static string defaultFormat = "yyyy-MM-dd"; ??????? /// <summary> ??????? /// 使用特定的名称生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="name">控件名称</param> ??????? /// <returns>Html文本</returns> ??????? public static string Calendar(this HtmlHelper helper, string name) ??????? { ??????????? return Calendar(helper, name, defaultFormat); ??????? } ??????? /// <summary> ??????? /// 使用特定的名称生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="name">控件名称</param> ??????? /// <param name="format">显示格式</param> ??????? /// <returns>Html文本</returns> ??????? public static string Calendar(this HtmlHelper helper, string name, string format) ??????? { ??????????? return GenerateHtml(name, null, format); ??????? } ??????? /// <summary> ??????? /// 使用特定的名称和初始值生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="name">控件名称</param> ??????? /// <param name="date">要显示的日期时间</param> ??????? /// <returns>Html文本</returns> ??????? public static string Calendar(this HtmlHelper helper, string name, DateTime date) ??????? { ??????????? return Calendar(helper, name, date, defaultFormat); ??????? } ??????? /// <summary> ??????? /// 使用特定的名称和初始值生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="name">控件名称</param> ??????? /// <param name="date">要显示的日期时间</param> ??????? /// <param name="format">显示格式</param> ??????? /// <returns>Html文本</returns> ??????? public static string Calendar(this HtmlHelper helper, string name, DateTime date, string format) ??????? { ??????????? return GenerateHtml(name, date, format); ??????? } ??????? /// <summary> ??????? /// 通过lambda表达式生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param> ??????? /// <returns>Html文本</returns> ??????? public static string CalendarFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) ??????? { ??????????? return CalendarFor(helper, expression, defaultFormat); ??????? } ??????? /// <summary> ??????? /// 通过lambda表达式生成控件 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param> ??????? /// <param name="format">显示格式</param> ??????? /// <returns>Html文本</returns> ??????? public static string CalendarFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string format) ??????? { ??????????? string name = ExpressionHelper.GetExpressionText(expression); ??????????? DateTime value; ??????????? object data = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, helper.ViewData).Model; ??????????? if (data != null && DateTime.TryParse(data.ToString(), out value)) ??????????? { ??????????????? return GenerateHtml(name, value, format); ??????????? } ??????????? else ??????????? { ??????????????? return GenerateHtml(name, null, format); ??????????? } ??????? } ??????? /// <summary> ??????? /// 通过lambda表达式获取要显示的日期时间 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param> ??????? /// <param name="format">显示格式</param> ??????? /// <returns>Html文本</returns> ??????? public static string CalendarDisplayFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string format) ??????? { ??????????? string name = ExpressionHelper.GetExpressionText(expression); ??????????? DateTime value; ??????????? object data = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, helper.ViewData).Model; ??????????? if (data != null && DateTime.TryParse(data.ToString(), out value)) ??????????? { ??????????????? return value.ToString(format); ??????????? } ??????????? else ??????????? { ??????????????? return string.Empty; ??????????? } ??????? } ??????? /// <summary> ??????? /// 通过lambda表达式获取要显示的日期时间 ??????? /// </summary> ??????? /// <param name="helper">HtmlHelper对象</param> ??????? /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param> ??????? /// <returns>Html文本</returns> ??????? public static string CalendarDisplayFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) ??????? { ??????????? return CalendarDisplayFor(helper, expression, defaultFormat); ??????? } ??????? /// <summary> ??????? /// 生成输入框的Html ??????? /// </summary> ??????? /// <param name="name">calendar的名称</param> ??????? /// <param name="date">calendar的值</param> ??????? /// <returns>html文本</returns> ??????? private static string GenerateHtml(string name, DateTime? date, string format) ??????? { ??????????? if (date != null) ??????????? { ??????????????? return "<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\" onfocus=\"WdatePicker({dateFmt:'" + format + "'})\" class=\"Wdate\" value=\"" + date.Value.ToString(format) + "\" />"; ??????????? } ??????????? else ??????????? { ??????????????? return "<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\" onfocus=\"WdatePicker({dateFmt:'" + format + "'})\" class=\"Wdate\" value=\"\" />"; ??????????? } ??????? } ??? } }
使用办法:
日期选择输入框:?<%= Html.CalendarFor(model => model.PlanStartTime)%>
显示日期:<%= Html.CalendarDisplayFor(model => model.PlanStartTime)%>
日期时间选择输入框:<%= Html.CalendarFor(model => model.PlanStartTime, “yyyy-MM-dd HH:mm”)%>
代码里面的公共类VeryCodes.DBUtility.DBHelperSQL怎么没有提供呢???呵呵,能不能帮发到我邮箱里面,看看。学习学习,呵呵。
谢谢支持。已经开源发布了,看这里:
http://blog.bossma.cn/csharp/verycodes_lib_control_open_source/
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论