I've got a problem when binding date value to textbox using knockout, as seen in the picture below
When the page is loaded for the first time, I'm using ajax to get the AccountStatements data.
function AccountStatementViewModel(companyID) {
var self = this;
...
var AccountStatement = {
AccountStatementID: self.AccountStatementID,
CompanyID: self.CompanyID,
Description: self.Description,
Amount: self.Amount,
ReceiptDate: self.ReceiptDate,
Type: self.Type
}
self.AccountStatement = ko.observable();
self.AccountStatements = ko.observableArray();
$.ajax({
url: webroot + 'AccountStatement/GetAccountStatements',
contentType: 'application/json; charset=utf-8',
data: { id: self.CompanyID },
cache: false
}).done(function (data) {
self.AccountStatements(data);
});
...
self.edit = function (accountStatement) {
$('#lnkAddAccountStatement').hide('blind', 1000);
$('#pnlAddEditAccountStatement').show('blind', 1000);
self.AccountStatement(accountStatement);
}
...
}
The Controller returns the result in json:
public JsonResult GetAccountStatements(int id)
{
var accountStatementsVM = db.AccountStatements
.Where(a => a.CompanyID == id)
.Select(a => new AccountStatementViewModel
{
AccountStatementID = a.AccountStatementID,
CompanyID = a.CompanyID,
Description = a.Description,
Amount = a.Amount,
ReceiptDate = a.ReceiptDate,
Type = a.Type
})
.ToList();
return Json(accountStatementsVM, JsonRequestBehavior.AllowGet);
}
ant the result is:
[{"AccountStatementID":2,"CompanyID":1,"Description":"test","Amount":1000,"ReceiptDate":"/Date(1447261200000)/","Type":"Payment"}]
In the View, I display it using this code:
<tbody data-bind="foreach: AccountStatements, visible: AccountStatements().length > 0">
<tr>
<td data-bind="attr: { id: AccountStatementID }">
<a href="#" class="btn btn-primary btn-xs" data-bind="click: $root.edit"><i class="glyphicon glyphicon-pencil"></i></a>
<a href="#" class="btn btn-danger btn-xs" data-bind="click: $root.delete"><i class="glyphicon glyphicon-remove"></i></a>
</td>
<td data-bind="text: Description"></td>
<td data-bind="text: Amount"></td>
<td data-bind="date: ReceiptDate"></td>
</tr>
</tbody>
Here is the code to format the date:
ko.bindingHandlers.date = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var valueUnwrapped = ko.utils.unwrapObservable(valueAccessor());
var textContent = moment(valueUnwrapped).format("DD/MM/YYYY");
ko.bindingHandlers.text.update(element, function () { return textContent; });
}
};
At this step, date is displayed in the right format, then if I clicked edit button, the ReceiptDate in the textbox is not formatted.
Code for ReceiptDate TextBox:
<input type="text" placeholder="Enter Receipt Date" class="form-control fdatepicker" readonly="readonly" data-bind="value: AccountStatement().ReceiptDate" />
If I change to data-bind="date: AccountStatement().ReceiptDate"
the textbox will be empty.
How to format the date in the textbox?
UPDATE
I've changed the date binding handler as in this link but the ReceiptDate's TextBox value is still /Date(1447261200000)/
Changes in the view:
<input type="text" placeholder="Enter Receipt Date" class="form-control fdatepicker" readonly="readonly" data-bind="date: AccountStatement().ReceiptDate" />
and date of receipt in the table is become empty:
<td data-bind="date: ReceiptDate"></td>