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 elementmodelerrorproperty when drillmodelstate.valuescollection haserrormessageproperty set message ("the value \"a\" \"my time\" invalid.") ,exceptionpropertynull. bound value ofmytimenull.this
errormessagedisplayed in validation summary of page.if enter invalid time,
"25:12", input elementmodelerrorproperty haserrormessageproperty set empty stringexceptionproperty set exception of typeinvalidoperationexceptioninner exception of typeoverflowexceptiontelling metimespannot analysed because 1 of numeric components out of valid range. bound value ofmytimenull.again,
errormessagedisplayed 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
Post a Comment