波斯码BOSSMA Information Technology

ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择

发布时间:2010年4月3日 / 分类:ASP.NET MVC / 26,605 次浏览 / 评论

在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”)%>

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯码,原文地址《ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

目前有2 条评论

  1. 风云 沙发:

    代码里面的公共类VeryCodes.DBUtility.DBHelperSQL怎么没有提供呢???呵呵,能不能帮发到我邮箱里面,看看。学习学习,呵呵。

发表评论