2

I have a ViewModel bound to my UI

<div class="editor-label-medium-bold">
    <div data-bind="text: 'Cancellation Amount'"></div>
</div>
<div class="editor-field-short">
    <div id="CancellationAmount" data-bind="text: CancellationAmount"></div>
</div>

The value for CancellationAmount does get displayed on when ko.applyBindings(xxx) is called. All good up to this point.

I have a JQueryUI datepicker, and upon date change I get an updated figure for the cancellation amount. This gets the right value back.

$.ajax({
    dataType: "json",
    method: "GET",
    url: '@Url.Action(MVC.XYZ.ActionNames.GetCancellationAmount, MVC.XYZ.Name)',
    data: { date: mdl.CancelDate(), policyid: '@ViewBag.PolicyID' }
})
.done(function (response) {
    mdl.CancellationAmount = ko.observable(response);
    $('#CancellationAmount').val(response);
})
.fail(function (response) {
    alert('fail');
});

Neither of the two lines in the "done" callback are updating the UI. I test this with Chrome and in the console, mdl.CancellationAmount() returns the correct figure, yet the UI wont budge.

I am on the verge of unbinding this field and manually update it with JQuery like I attempt on the second line of the "done" callback. I think because the field is bound, knockout stops JQuery (maybe anything else...) from updating it.

I should probably mention this line of code:

mdl.CancellationAmount = ko.observable(mdl.AnnualPremium());

On initial load, my model from the server sends null for CancellationAmount, so I copy another value to it which is a positive decimal figure, but I unwrap that, so dont think that is the issue. This line is just before ko.applyBindings(xxx)

Has anyone seen this before, or has any idea how to overcome this issue?

4

1 回答 1

4

mdl.CancellationAmount您以错误的方式分配数据。done它已经是可观察的,因此您应该使用 () 在函数中赋值。重写你的回调如下:

.done(function (response) {
    mdl.CancellationAmount(response);
})
于 2013-07-08T15:41:47.620 回答