使用Knockout和Asp.net MVC4显示模块模式

时间:2013-02-17 00:37:30

标签: javascript jquery asp.net-mvc-4 knockout.js knockout-2.0

我正在尝试使用我的asp.net mvc 4应用程序淘汰赛。这就是我的代码。

    <script>


var my = my || {}; //creating private namespace to avoid any conflicts other namespaces: my namespace

$(document).ready(function () {



    ////////////////view model testing////////////////////////////

    // Define Main ViewModel; javascript Object Literals
    ////it is a workaround for moduler JS pattern including revealing js pattren 
    ///it also uses KnockOut. end product ViewModel; 

    function teammemberModel() {
        this.Id = ko.observable();
        this.Title = ko.observable();
        this.Name = ko.observable();
        this.Email = ko.observable();
        this.Nationality = ko.observable();
        this.Sex = ko.observable();

    };


    my.viewModel = function () {

        var teamMembers = ko.observableArray([]),

            loadTeamMembers = function (projectId) {                
                $.ajax({
                    type: "GET",
                    url: "/Project/GetTeamMembers?projectId=" + projectId,

                    success: function (response) {

                        my.viewModel.teamMembers.removeAll();
                        $.each(response.results, function (x, team) {
                            my.viewModel.teamMembers.push(new teammemberModel()
                                .Id(team.Id)
                                .Title(team.Title)
                                .Name(team.UserName)
                                .Email(team.Email)
                                .Nationality(team.Nationality)
                                .Sex(team.Sex)

                            );



                        });
                    }                        
                });
            }
        return {
            teamMembers: teamMembers,
            loadTeamMembers: loadTeamMembers
        };
    } ();

    //Applies KO bindings        
    ko.applyBindings(my.viewModel);
    my.viewModel.loadTeamMembers(6);

    ///////////////////////////////////////////////////       

    });    
    </script>

这是我的客户端Knockout基本视图模型的模块化JS实现示例。我的观点如下。

    <table >    
    <thead>
    <tr>

        <th>Title</th>
        <th>Description</th>
        <th>Status</th>
        <th>Created By</th>
        <th>Created Date</th>
    </tr>
</thead>    
<tbody data-bind="foreach: teamMembers">

    <tr>                            
        <td data-bind="text: UserName"></td>
        <td data-bind="text: Email"></td>
        <td data-bind="text: Sex"></td>
        <td data-bind="text: Title"></td>
        <td data-bind="text: Nationality"></td>

    </tr>                        

      </tbody>
    </table>

我可以看到json数据在我的ajax调用中被推入teamMembers ko.observableArray。这段代码应该按照我想要遵循的教程工作,但我没有在我的表中显示我的任何数据。有人可以指导我这个代码有什么问题,以及为什么我的表没有在这里呈现。感谢

1 个答案:

答案 0 :(得分:0)

您的客户端代码看起来很好可能是您的服务器端代码有问题。检查从服务器返回的内容,尤其是在响应变量中返回的数据。

我认为response.result没有任何数据,这就是你获得异常的原因。所以请调试您的客户端代码。

$.each(response.results, function (x, team) {}