Reputation: 101
I have kendo datetimepicker in kendo grid popup editor. I describe field in model with format:
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
And in EditorTemplate
@Html.Kendo().DateTimePickerFor(model => model.Date).Value(DateTime.Now).Format("dd.MM.yyyy HH:mm")
I also have DateTimeBinder
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (!string.IsNullOrEmpty(displayFormat) && value != null && !String.IsNullOrWhiteSpace(value.AttemptedValue))
{
DateTime date;
displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
else
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format("{0} Incorrect Format", value.AttemptedValue));
}
}
return base.BindModel(controllerContext, bindingContext);
}
problem is when I save this popup and reaches the model in the controller server side it is with format dd.MM.yyyy H:mm:ss not dd.MM.yyyy HH:mm:ss. for example, if I save with this time 31.03.2015 08:56, after submitting in server, it becomes 31.03.2015 8:56. Have you had case like this??
Upvotes: -1
Views: 1187
Reputation: 12050
You can use this approach below that is working without any problem.
Model:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Start Date")]
public DateTime? StartDate{ get; set; }
View:
@Html.LabelFor(m => m.StartDate)
@(Html.Kendo().DatePickerFor(m => m.StartDate)
.Animation(true)
.Culture("tr-TR") //Set culture
.Footer(false)
.Format("dd.MM.yyyy")
.Min(new DateTime(1900, 1, 1)) //Set min date of the datepicker
.Max(new DateTime(2099, 12, 31)) //Set min date of the datepicker
//.Value(DateTime.Today) //Set the value of the datepicker
//.HtmlAttributes(new { @class = "k-datepicker" })
)
I think you do not need DateTimeBinder
Upvotes: 0