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 elementmodelerror
property when drillmodelstate.values
collection haserrormessage
property set message ("the value \"a\" \"my time\" invalid."
) ,exception
propertynull
. bound value ofmytime
null
.this
errormessage
displayed in validation summary of page.if enter invalid time,
"25:12"
, input elementmodelerror
property haserrormessage
property set empty stringexception
property set exception of typeinvalidoperationexception
inner exception of typeoverflowexception
telling metimespan
not analysed because 1 of numeric components out of valid range. bound value ofmytime
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
Post a Comment