jquery - Modelstate always throws invalid datetime error -
i have question date validations in asp.net mvc4. using data annotations date validations follows
[datatype(datatype.date)] [allowhtml] [displayformat(htmlencode = false, applyformatineditmode = true, dataformatstring = "{0:dd/mm/yyyy}")] [display(name = "date of birth")] public nullable<system.datetime> dateofbirth { get; set; }
next, in html page, using following in mvc html form
@html.textboxfor(model => model.usersettings.dateofbirth, string.format("{0:dd/mm/yyyy}", model.usersettings.dateofbirth), new { id = "dob", @class = "brth-ipt" })
lastly, using jquery date picker follows
$(function () { $("#dob").datepicker("destroy"); $("#dob").datepicker({ showon: "both", beforeshow: customrange, dateformat: "dd/mm/yy", //"mm-dd-yy", firstday: 1, changefirstday: false, changemonth: true, changeyear: true, yearrange: "-150:+0" }); });
my problem is, when select date datepicker, displayed mm/dd/yyyy format , works fine.
but when load page again edit form, displaying in mm-dd-yyyy format. now, when save form without chaging previous date (mm-dd-yyyy format), not able save form data annotation vaidation throwing me error. "date not in valid format".
i not able solve issue.
try this:
[datatype(datatype.date)] [displayformat(applyformatineditmode = true, dataformatstring = "{0:d}")] [display(name = "date of birth:")] public datetime? dateofbirth { get; set; }
as far know, wouldn't need use jquery date picker
. framework comes built in helper pick dates.
in view
<div class="editor-label"> @html.labelfor(model => model.dateofbirth ) </div> <div class="editor-field"> @html.editorfor(model => model.dateofbirth ) @html.validationmessagefor(model => model.dateofbirth ) </div>
this tested in google chrome
Comments
Post a Comment