We are finding that the DatePicker is causing us real problems when trying to use UK date formats. We have had to implement our own binder for formatting dates:
http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx
But are now getting odd parsing errors with the DatePicker when the date is no US convertible (e.g. 29 May 2010):
| <%= Html.Telerik().DatePicker() |
| .Name("Date") |
| .Value(Model.Date) |
| %> |
Throws the error:
The parameter conversion from type 'System.String' to type 'System.DateTime' failed. See the inner exception for more information.
{"29/05/2010 is not a valid value for DateTime."}
Whereas a date that can be reversed works fine (e.g. 11 May 2010).
Including the following seems to have no effect:
Html.Telerik().ScriptRegistrar().Globalization(
true);
A complete code sample of how to approach this would be appreciated.
7 Answers, 1 is accepted
I think you don't need to use custom binders - you just need the CultureInfo.CurrentCulture to be properly set. I am sending you a sample application which shows how to do that. Check Global.cs for the implementation.
Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Thanks for this - however, this works fine with a POST, not with a GET. I can't attach a zip file, so here is the code in the revised view:
| <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" > |
| <head runat="server"> |
| <title>Index</title> |
| <%= Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => |
| group.Add("telerik.common.css") |
| .Add("telerik.web20.css") |
| ) %> |
| </head> |
| <body> |
| <div> |
| <% using (Html.BeginForm("Index", "Home", FormMethod.Get)) |
| {%> |
| <%= Html.Telerik().DatePicker() |
| .Name("myDate") |
| .Value(DateTime.Now.Date) |
| %> |
| <input type="submit" value="Get" /> |
| <% Html.EndForm(); %> |
| <%= ViewData["myDate"]%> |
| </div> |
| <%} %> |
| <%= Html.Telerik().ScriptRegistrar().Globalization(true) %> |
| </body> |
| </html> |
Thanks
Tom
I was able to reproduce this behavior however I am not sure it is related with the datepicker. The same thing happened with a regular textbox:
<% Html.BeginForm("Index", "Home", FormMethod.Get); %>
<%= Html.TextBox("myDate",DateTime.Now.Date) %>
<input type="submit" value="get" />
<% Html.EndForm(); %>
<%= ViewData["myDate"]%>
It seems that for some reason the wrong cultureinfo is being picked up. Unfortunately I cannot tell why.
Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
After checking again the blog post it seems this is kind of expected. I will try to prepare a solution using a custom binder and send you the project.
Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Yes, when using the binder everything works as expected, apart from the error I reported in the original post.
I am back :)
Find attach a working project. The only difference is that the model binder is for DateTime? instead of DateTime. If DateTime is required you could modify the model binder.
Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Turns out I was missing one simple bit of logic in the customer binder:
return DateTime.Parse(date, CultureInfo.GetCultureInfo("en-GB"), DateTimeStyles.None);
rather than:
return DateTime.Parse(date);