简体   繁体   English

Knockout.js通过使用push方法添加数据而不更新视图

[英]Knockout.js Adding data by using push method not updating view

I'm getting data from WebApi on JSON format and then adding received data to MVC View by using .push() method of KnockoutJS. 我从JSON格式的WebApi获取数据,然后使用KnockoutJS的.push()方法将接收到的数据添加到MVC View。 The JSON data I received on POST response is correct, so I believe it's something wrong on client side - instead of data I'm geting undefined and [Object] . 我在POST响应中收到的JSON数据是正确的,因此我认为在客户端这是有问题的-而不是我得到undefined[Object] Although after page refresh all data showing correctly. 尽管刷新页面后所有数据仍正确显示。 Here my knockout code: 这是我的剔除代码:

<script>
    var viewModel = {
        prepp: ko.observableArray(),
        currentPage: ko.observable(-1)
    };
    $(function () {
        getData(viewModel.currentPage() + 1);
        ko.applyBindings(viewModel, document.getElementById("prepps"));      
        });
        //This function used for paging, not concern to question directly
        function getData(pageNumber) {
            if (viewModel.currentPage() != pageNumber) {
                $.ajax({
                    url: "/api/index",
                    type: "get",
                    contentType: "application/json",
                    data: { id: pageNumber }
                }).done(function (data) {
                    if (data.length > 0) {
                        viewModel.currentPage(viewModel.currentPage() + 1);
                        for (var i = 0; i < data.length; i++) {
                            viewModel.prepp.push(data[i]);
                        }
                    }
                });
            };
            //Here we call POST action of WebApi.
            $(".send-feed").click(function () {
                var guid = getguid();
                var styles;
                var req = { RequestName: $("#request").val(), RequestDescription: $("#request-review").val(), RequestOwner: $("#username").val(), RequestGuid: guid, RequestStyles: [] }
                $("div.click").each(function () {
                    styles = { RequestGuid: guid, StyleId: $(this).text() };
                    req.RequestStyles.push(styles);
                });
                var model = JSON.stringify(req);
                $.ajax({
                    url: "/api/index",
                    type: "POST",
                    contentType: "application/json, charset: utf-8",
                    data: model
                }).done(function (data) {
                    viewModel.prepp.push(data);
                });

            });
        }
    });
</script>

And here is the MVC View markup: 这是MVC视图标记:

div class="prepp-blocks-container" data-bind="foreach: prepp" id="prepps">
    <div class="prepp-block">
        <div class="star" data-bind="if: $data.IsStylistOffer == true">
            <img src="../../img/star-yellow.png" alt="added by stylist">
        </div>
        <a data-bind="attr: {href: 'Request/' + $data.RequestGuid}"><h3 data-bind="text: $data.RequestName"></h3></a>
        <span  data-bind="foreach: {data: RequestStyles, as: 'style'}">
            <div data-bind="text: style" class="taste-prepp"></div>
        </span>
        <p class="text-small-grey" data-bind="text: $data.PreppsNumber + ' prepps'"></p>
    </div>
</div>

I believe your return type from controller should be adjusted so it will match the view model structure like 我相信您应该调整控制器的返回类型,使其与视图模型结构类似

public model Get() 
{ 
//build your list .
return model ; 
}

Try to use ko.mapping.toJS() so knockout advantages are not lost . 尝试使用ko.mapping.toJS()这样就不会失去淘汰优势。

Refer knockout doc's you can find more relevant info how we can better use it Here 请参考敲除文档,您可以找到更多相关信息,我们将如何在此处更好地使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM