This is a migrated thread and some comments may be shown as answers.

[Solved] DatePicker type conversion error

3 Answers 43 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael
Top achievements
Rank 1
Michael asked on 22 Jan 2011, 05:47 PM
If a user types an invalid string into the date picker and submits it for server validation, and then re-display the view with the model errors, the datepicker fails to parse the (invalid) string value and throws a datetime parse exception.

Go to http://demos.telerik.com/aspnet-mvc/datepicker/servervalidation and enter "s" into the first field and hit Save. Instead of getting an error back indicating "s" in an invalid time/date, you get redirected to an error page.

From some poking around in the code, it looks like the problem is that the extension method GetValue<T> on IInputComponent assumes that if the value is in the ModelState, it must be convertible to T. But if there have been model binding errors the value represented by item in ModelState will not in fact be convertible to T.

Changing:
if (viewData.ModelState.TryGetValue(instance.Id, out state) && (state.Value != null))
{
 value = state.Value.ConvertTo(typeof (T), CultureInfo.CurrentCulture) as T?;
}
To
if (viewData.ModelState.TryGetValue(instance.Id, out state) && (state.Value != null))
{
 if (viewData.ModelState.IsValidField(instance.Id))
 {
   value = state.Value.ConvertTo(typeof (T), CultureInfo.CurrentCulture) as T?;
 }
 else
 {
   value = converter(state.Value.RawValue);
 }
}
 In
public static T? GetValue<T>(this IInputComponent<T> instance, Func<object, T?> converter)

Seems to fix the underhandled exception, but it doesn't allow the invalid value to be preserved (instead, the DateTime is null, so in my case it ends up being today). I put together a change that will check if the model contains a valid value of the date and use the attempted value if it doesnt... I'm not that happy with it but I'm happy to post it if anyone wants.

A better change in general might be to do the same thing the numeric inputs do, and ensure that you can never type an invalid date into a datepicker. (I'm pretty sure the numeric text boxes suffer from the same problem, in that if you were using server validation with an invalid value in the model the textbox would fail to render it, as it wouldn't be able to convert the attempted value to a int/double,  but it is fairly hard to create that scenario as the numeric textboxes simply don't allow you to type a string that will later fail.)

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 24 Jan 2011, 05:23 PM
Hello Michael,

Thank you for drawing our attention to this issue.

I was able to observe it locally and I confirm it as a bug. I am glad to inform you that this issue is already fixed and the fix will be included in the next official release of Telerik Components for ASP.NET MVC.

I have updated your Telerik points. 

Regards,
Georgi Krustev
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
0
Dave
Top achievements
Rank 1
answered on 24 Jan 2011, 07:45 PM
Since you just released SP1 is there any way to get the fix ASAP?
0
Georgi Krustev
Telerik team
answered on 25 Jan 2011, 08:55 AM
Hello Michael,

Latest internal builds which includes the fixes after Service packs are available only for clients. SP1 (version 2010.3.1318) was introduced to all because of the official release of ASP.NET MVC 3.

Kind regards,
Georgi Krustev
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
Tags
Date/Time Pickers
Asked by
Michael
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Dave
Top achievements
Rank 1
Share this question
or