This question is locked. New answers and comments are not allowed.
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:
To
In
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.)
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?;}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); }}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.)