c# - Knockout not populating DateTime? when initial is null -
i'm using knockout bind view model view. multiple properties in view model nullable, such datetime?
s. here's example:
public class viewmodel { public int id { get; set; } public string name { get; set; } public datetime? creationdate { get; set;} }
as can see, property creationdate
nullable datetime
.
i'm binding property custom datepicker binder:
ko.bindinghandlers.datepicker = { init: function (element, valueaccessor, allbindingsaccessor, viewmodel) { try { var jsondate = ko.utils.unwrapobservable(valueaccessor()); var value = parsejsondatestring(jsondate); var strdate = value.getmonth() + 1 + "/" + value.getdate() + "/" + value.getfullyear(); element.setattribute('value', strdate); } catch (exc) { } $(element).change(function () { var value = valueaccessor(); value(element.getattribute('value')); }); }, update: function (element, valueaccessor, allbindingsaccessor, viewmodel) { var val = valueaccessor(); val(element.getattribute('value')); } }; var jsondatere = /^\/date\((-?\d+)(\+|-)?(\d+)?\)\/$/; var parsejsondatestring = function (value) { var arr = value && jsondatere.exec(value); if (arr) { return new date(parseint(arr[1])); } return value; };
this enables me bind property in view so:
<input type="text" data-bind="datepicker: creationdate" />
problem
here's problem. property null when enters view. json example this:
{ "id": 2004, "name": "test", "creationdate": null }
if case, , change value random value datepicker, , send ajax post controller, can see models creationdate
still equal null
.
so if datetime
null model enters view, how populate models property?
found solution on own
i managed solve issue changing binding following:
ko.bindinghandlers.datepicker = { init: function (element, valueaccessor, allbindingsaccessor) { //initialize datepicker optional options var options = allbindingsaccessor().datepickeroptions || {}; $(element).datepicker(options); //handle field changing ko.utils.registereventhandler(element, "change", function () { var observable = valueaccessor(); var value = $(element).val(); // if input field empty, value falsy , therefore observable should = null if(!value){ observable(null); } else { var date = new date(value); observable(date); } }); //handle disposal (if ko removes template binding) ko.utils.domnodedisposal.adddisposecallback(element, function () { $(element).datepicker("destroy"); }); }, //update control when view model changes update: function (element, valueaccessor) { var value = ko.unwrap(valueaccessor()); //if value received null, should display nothing in input field if (value === null) { $(element).val(null); } else { //we need manipulate data show user friendly user var date = parsejsondatestring(value); var strdate = date.getmonth() + 1 + "/" + date.getdate() + "/" + date.getfullyear(); $(element).val(strdate); } } };
so if value i'm getting in update
function, set $(element).val(null)
. way, nullable properties handled correctly.
Comments
Post a Comment