asp.net - Display validation error when TimeSpan model binding exceptions occur -


in asp.net mvc 4 application have view model contains nullable timespan property:

[displayname("my time")] public timespan? mytime { get; set; } 

it bound input element in view:

@html.editorfor(model => model.mytime) 

the input box gets rendered of custom editor template timespan.cshtml:

@model nullable<system.timespan>  @html.textbox("", (model.hasvalue     ? model.value.tostring(@"hh\:mm") : string.empty),     new { @class = "text-box single-line hastimepicker" data_timepicker = true }) 

now, if enter following 2 kinds of invalid times , submit page following different behaviour of model binder:

  • if enter letter, "a" input element modelerror property when drill modelstate.values collection has errormessage property set message ("the value \"a\" \"my time\" invalid.") , exception property null. bound value of mytime null.

    this errormessage displayed in validation summary of page.

  • if enter invalid time, "25:12", input element modelerror property has errormessage property set empty string exception property set exception of type invalidoperationexception inner exception of type overflowexception telling me timespan not analysed because 1 of numeric components out of valid range. bound value of mytime null.

    again, errormessage displayed in validation summary of page. because empty it's not useful.

ideally second case of invalid input prefer have same kind of error message first case, example "the value \"25:12\" \"my time\" invalid.".

how can solve problem?

edit

a custom validation attribute apparently doesn't here because not called invalid input in examples above when model binder detects invalid values. had tried approach without success.

the problem error happening @ model binding , that's need catching , checking it.

i have timespan model binder , editor template timespan? should need that's up on gist


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -