asp.net mvc - Trouble with ModelState on datetime field -
in our asp mvc 3 site, want user able enter incomplete form, save record database, , come form later. if there fields entered incorrectly want error message appear when user hits save.
a problem occurring specific date of birth field, however. if user enters incorrect date such 14/01/2011
, when user hits save , model object posts controller action, date of birth comes through blank. fails if (modelstate.isvalid)
condition, not save database, , sent view blank date of birth.
is there way allow user enter incorrect date in field, keep value , post view value , error message generated model? since using jquery input mask, value entered numeric , in mm/dd/yyyy format.
model
[displayname("date of birth")] [displayformat(dataformatstring = "{0:mm/dd/yyyy}", applyformatineditmode = true)] public datetime? birthdate { get; set; }
controller
[httppost] public actionresult edit(monet.models.agenttransmission agenttransmission) { //date of birth field null right here in 'agenttransmission' object if //user enters incorrect value/date //redirect if security not met. if (!security.ismodifier(user)) return redirecttoaction("message", "home", new { id = 1 }); //tax id security viewbag.fullssn = security.isfullssn(user); try { agenttransmission = agenttransmissionvalidator.reformatfields(agenttransmission); viewbag.errormessage = agenttransmissionvalidator.validateagent(agenttransmission); if (modelstate.isvalid) { //whole bunch of code determine if input complete/incomplete //save record db.entry(agenttransmission).state = entitystate.modified; db.savechanges(); } //end if model state valid } catch (dbentityvalidationexception dbex) { viewbag.errormessage = "an error has occurred, apologize incovenience. has been notified , resolve issue shortly."; sendemail.errormail(common.errorcheck.combinedberrors(dbex)); } catch (exception ex) { viewbag.errormessage = errorcheck.friendlyerror(ex); sendemail.errormail(ex); } return view(agenttransmission); }
view
<div class="m-editor-label"> @html.labelfor(model => model.birthdate)<span class="req">*</span> </div> <div class="m-editor-field"> @html.editorfor(model => model.birthdate) @html.validationmessagefor(model => model.birthdate) </div>
as queti mporta suggested, problem that, when post back, model binder tries parse "14/01/2011" datetime?
, fails, uses default value instead. nullable type, default null
, why blank.
if change model to...
[displayname("date of birth")] [displayformat(dataformatstring = "{0:mm/dd/yyyy}", applyformatineditmode = true)] public string birthdate { get; set; } // note: string instead of datetime?
...the user can enter format mm/dd/yyyy, when posted back, stored in string.
you need add logic (in controller or somewhere accessible controller) attempt parse yourself:
datetime birthdate; var isdatevalid = datetime.tryparse(agenttransmission.birthdate, out birthday); if (!isdatevalid ) modelstate.addmodelerror("birthdate", "birthday needs valid date.");
Comments
Post a Comment