MVC日期时间模型绑定 [英] MVC Date Time Model Binding
问题描述
我在应用程序中使用了2个剑道日期选择器,例如:
I am using 2 kendo date pickers in my application as such:
<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.
When the button is clicked, I read the values of these date pickers client side and make a POST to a API controller.
我遇到的问题有时是DateTime参数解析不正确,我使用的是en-GB文化(在我的web.config中指定),但是给定的日期为2014年3月1日(3月1日),该值由模型联编程序处理,解释为2014年3月1日(1月3日).
The issue I am having is sometimes the DateTime parameters are parsed incorrectly, I am using a en-GB culture (specified in my web.config), however given a date of 01/03/2014 (1st March), when the value is processed by the model binder, it is interpreted as 03/01/2014 (3rd Jan).
我的JavaScript如下:
My javascript is as follows:
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是自定义对象的属性.
I figured the solutions was that I needed a custom model binder to parse the datetime value, so I created on (as follows) and registered it in the Global.asax.cs file, but this didnt work, the binder is never called, I am unsure if this is because the datetime is a property of a custom object.
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);
有人知道我要去哪里吗?
Does anyone know where I am going wrong?
推荐答案
我没有看到的是您在global.asax中注册DateTimeModelBinder的位置:
What I didn't see was where you registered your DateTimeModelBinder in your global.asax:
ModelBinders.Binders[typeof(DateTime)] =
new DateAndTimeModelBinder() { CustomFormat = "yyyy-mm-dd" };
Scott Hanselman的这篇与DateTime自定义模型绑定器非常相似的帖子
这篇关于MVC日期时间模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!