Knockout没有绑定更新值

时间:2013-07-16 09:54:58

标签: knockout.js knockout-2.0

我有一个淘汰模型并将其与我的asp.net mvc视图绑定。这是我的模特

function SearchListingViewModel() {
var self = this;
self.selectedBedRooms = ko.observableArray([]);
self.minPrice = ko.observable();
 self.search = function () {   
    var data = {
        Status: self.selectedStatus(),
        MinPrice: self.minPrice(),
        MaxPrice: self.maxPrice(),
        BedRooms: self.selectedBedRooms(),
        PageNumber: 0,
        PageSize : self.pageSize()
    };

    $.post("/listing/searchlistings", data)
    .done(function (json) {
        self.Listings(json.listings);
        json = json || {};
        if (json.success) {
            //Inserting commas in all listing's price --- Start

            $(json.listings).each(function () {
                var selfthis = this;
                console.log(selfthis.Price);

                selfthis.Price = "45,352,2"; // You can see i am hardcoding price here 


                console.log(selfthis.Price);
            });

            //Inserting commas in all listing's price --- End

            self.Listings(json.listings);
            self.pages(json.pages);
            self.pageCount(json.pageCount);
            if (self.Listings().length <= 0) {
                Alert.Information('No results found!');
            }
        } else if (json.errors) {
            Alert.Error(json.errors[0]);
        }
    });

}

我的观点

<div data-bind="foreach : Listings"> 
    <span data-bind="text:Price"></span>
</div>

Json数据

 {Price:555555, Commission:0, MarketingDescription:null, ListingStatusId:1, PlaceId:32, Place:null,…}

当它呈现时,它会在屏幕上显示

 555555 43434343 5454545454 4563665 343 

而不是

45,352,2 45,352,2 45,352,2 45,352,2 45,352,2 

出了什么问题?

提前致谢

2 个答案:

答案 0 :(得分:0)

据我所知,我相信你访问的是从json数据设置的商品价格,这是在控制台中打印的。

尝试更改

selfthis.Price = "45,352,2";

要     self.Listings.Price = "45,352,2";

答案 1 :(得分:0)

啊,这是我身边的问题,

.done(function (json) {
    self.Listings(json.listings); // here i am setting listings into observable array before editing this was the problem 
    json = json || {};

删除此行后,它可以正常工作。