Код: Выделить всё
Start Date:
@(Html.Kendo().DatePickerFor(m=>m.StartDate))
End Date:
@(Html.Kendo().DatePickerFor(m=>m.EndDate))
Show
Проблема, с которой я сталкиваюсь, заключается в том, что параметры DateTime иногда анализируются неправильно, я использую культуру en-GB (указанную в моем web.config), однако с учетом даты 03.01.2014 (1 марта) , когда значение обрабатывается связывателем модели, оно интерпретируется как 01.03.2014 (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
///
/// Initializes a new instance of the class.
///
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
///
/// Gets or sets the end date.
///
public DateTime EndDate { get; set; }
///
/// Gets or sets the start date.
///
public DateTime StartDate { get; set; }
#endregion
}
Код: Выделить всё
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);
Подробнее здесь: https://stackoverflow.com/questions/224 ... el-binding
Мобильная версия