【问题标题】:MVC Date Time Model BindingMVC 日期时间模型绑定
【发布时间】:2014-04-20 23:12:55
【问题描述】:

我在我的应用程序中使用了 2 个剑道日期选择器:

<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 年 1 月 3 日(3 月 1 日),当该值由模型绑定器处理,它被解释为 03/01/2014 (3rd Jan)。

我的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 文件中,但这不起作用,绑定器永远不会调用,我不确定这是否是因为日期时间是自定义对象的属性。

 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);

有人知道我哪里出错了吗?

【问题讨论】:

标签: c# asp.net-mvc model-binding custom-model-binder kendo-datepicker


【解决方案1】:

我没有看到您在 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

【讨论】:

  • 感谢您,我最终将参数作为 UTC 时间戳发布到控制器,然后繁荣,MVC 自动将时间戳转换为有效的日期时间,因此我的模型可以很好地绑定:)跨度>
猜你喜欢
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多