加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – MVC日期时间模型绑定

发布时间:2020-12-16 00:10:48 所属栏目:百科 来源:网络整理
导读:我在我的应用程序中使用2个kendo日期选择器: div class="span12" div class="span2" style="text-align: right" Start Date: /div div class="span2" @(Html.Kendo().DatePickerFor(m=m.StartDate)) /div div class="span2" style="text-align: right" End
我在我的应用程序中使用2个kendo日期选择器:

<div class="span12">
    <div class="span2" style="text-align: right">
        Start Date:
    </div>
    <div class="span2">
        @(Html.Kendo().DatePickerFor(m=>m.StartDate))
    </div>
    <div class="span2" style="text-align: right">
        End Date:
    </div>
    <div class="span2">
        @(Html.Kendo().DatePickerFor(m=>m.EndDate))
    </div>
    <div class="span4">
        <button class="btn btn-primary" onclick="getGraphData()">Show</button>
    </div>
</div>

单击该按钮时,我会读取这些日期选择器客户端的值,并向API控制器发出POST.

我遇到的问题有时是DateTime参数被错误地解析,我使用的是en-GB文化(在我的web.config中指定),但是给定日期为2014年3月1日(3月1日),当值为由模型装订器处理,它被解释为03/01/2014(1月3日).

我的javascript如下:

function getGraphData() {

        var startDatePicker = $("#StartDate").data("kendoDatePicker");
        var endDatePicker = $("#EndDate").data("kendoDatePicker");
        var param = {
            StartDate: kendo.toString(startDatePicker.value().toLocaleDateString(),"dd/MM/yyyy"),EndDate: kendo.toString(endDatePicker.value().toLocaleDateString(),"dd/MM/yyyy")
        };
       // Do post here

    }

我的模型如下:

public class DateRangeParam
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DateRangeParam"/> class.
        /// </summary>
        public DateRangeParam()
        {
            this.EndDate = DateTime.Today.AddDays(1).AddSeconds(-1);
            this.StartDate = new DateTime(DateTime.Now.Year,DateTime.Now.Month,1);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the end date.
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        ///     Gets or sets the start date.
        /// </summary>
        public DateTime StartDate { get; set; }

        #endregion
    }

我认为解决方案是我需要一个自定义模型绑定器来解析日期时间值,所以我创建了(如下所示)并将其注册在Global.asax.cs文件中,但是这不起作用,绑定从不被调用,我我不确定这是因为datetime是自定义对象的属性.

public class DateTimeModelBinder : IModelBinder
    {
        #region Fields


        private readonly string _customFormat;

        #endregion

        #region Constructors and Destructors

       public DateTimeModelBinder(string customFormat)
        {
            this._customFormat = customFormat;
        }

        #endregion

        #region Explicit Interface Methods

        object IModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
        {
            ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            return DateTime.ParseExact(value.AttemptedValue,this._customFormat,CultureInfo.InvariantCulture);
        }

        #endregion
    }

它注册如下:

var binder = new DateTimeModelBinder(new CultureInfo("en-GB").DateTimeFormat.ShortDatePattern);
ModelBinders.Binders.Add(typeof(DateTime),binder);
ModelBinders.Binders.Add(typeof(DateTime?),binder);

有谁知道我哪里出错了?

解决方法

我没看到的是你在global.asax中注册DateTimeModelBinder的地方:

ModelBinders.Binders[typeof(DateTime)] = 
           new DateAndTimeModelBinder() { CustomFormat = "yyyy-mm-dd" };

Scott Hanselman has this very similar post working with DateTime Custom Model Binders

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读