javascript - Date format in web api & Ajax application -
i have web api 2 application in have issue:
in viewmodel
:
[datatype(datatype.date)] [displayformat(dataformatstring = "dd/mm/yyyy")] [display(name = "date de naissance ")] public nullable<system.datetime> birth_date { get; set; }
in view :
<input type="date" id="editbirth_date" name="birth_date" placeholder="dd/mm/yyyy" />
in script :
$.ajax({ type: "get", url: "/api/client/getclient/" + selectedradio.attr('value'), success: function (data) { $('#editid').val(data.id); $('#editcnss').val(data.cnss); $('#editlastname').val(data.lastname); $('#editfirstname').val(data.firstname); $('#editadresse').val(data.address); $('#editbirth_date').val(data.birth_date); $('#editbirth_place').val(data.birth_place); $('#editmutuelle').val(data.mutuelle); $('#editcaisse_assu_maladie').val(data.caisse_assu_maladie); $('#editclient_type').val(data.client_type); selectview("edit"); } });
and finally, in controller :
[httpget] public clientmodel getclient(int id) { ajt_client entity = repo.getclientbyid(id); clientmodel item = mapper.map<ajt_client, clientmodel>(entity); return item; }
when debug code 27/02/2015 00:00:00
value of item.birth_date.but in view empty string !!!
i need know :
- why happens ?
- how can fix code?
i think got error in console :
the specified value 'invalid date' not conform required format, 'yyyy-mm-dd'.
indeed, your date not in required format, need transform before : 2015-02-27 (instead of 27/02/2015)
the best solution api should return real date object , not formated string. , in client side, should able parse real date in required format yyyy-mm-dd
you can manipulate javascript dates lib : momentjs
Comments
Post a Comment