在MVC中My97date应用C#扩展方法

项目中用的my97date,其他字段是用 @Html.EditorFor(model => model.字段)绑定的,但日历控件是用<input />绑定的,但这样写并不是特别好。

为了也能用@HTML绑定属性,用了扩展方法。代码如下:

public static class My97DatePicker
    {
        private static string defaultFormat = "yyyy-MM-dd";

        /// <summary>  
        /// 使用特定的名称生成控件  
        /// </summary>  
        /// <param name="helper">HtmlHelper对象</param>  
        /// <param name="name">控件名称</param>  
        /// <returns>Html文本</returns>  
        public static MvcHtmlString 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 MvcHtmlString 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 MvcHtmlString 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 MvcHtmlString 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 MvcHtmlString 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 MvcHtmlString 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 MvcHtmlString 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 new MvcHtmlString(value.ToString(format)) ;
            }
            else
            {
                return new MvcHtmlString("");

            }

        }



        /// <summary>  
        /// 通过lambda表达式获取要显示的日期时间  
        /// </summary>  
        /// <param name="helper">HtmlHelper对象</param>  
        /// <param name="expression">lambda表达式,指定要显示的属性及其所属对象</param>  
        /// <returns>Html文本</returns>  
        public static MvcHtmlString 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 MvcHtmlString GenerateHtml(string name, DateTime? date, string format)
        {
            string str = "";
            if (date != null)
            {
                str= "<input type=\"text\" id=\"" + name + "\"  onclick=\"WdatePicker({el:'" + name + "'})\" class=\"Wdate\" value=\"" + date.Value.ToString(format) + "\" />";
            }
            else
            {
                str= "<input type=\"text\" id=\"" + name + "\"  onclick=\"WdatePicker({el:'" + name + "'})\" class=\"Wdate\" value=\"\" />";
            }
            return new MvcHtmlString(str);
        }
    }

只要引用该方法的命名空间,我们就能愉快的使用@Html.CalendarFor等方法。当然还需要引用My97date的JS文件。

 

posted @ 2016-09-20 17:26  SpeedHorse  阅读(423)  评论(0编辑  收藏  举报