无法用mvc视图绑定knockoutjs

时间:2014-01-08 17:28:10

标签: asp.net-mvc-4 twitter-bootstrap knockout.js knockout-mapping-plugin

我是knockoutjs的新手,我试图绑定视图,但不能。来自服务器的数据很好,ajax工作正常,但没有绑定问题。

这是我的js代码:

var UserViewModel = function () {
var self = this;
//Declare observable which will be bind with UI 
self.Id = ko.observable("0");
self.FirstName = ko.observable("");
self.LastName = ko.observable("");

//The Object which stored data entered in the observables
var UserData = {
    Id: self.Id || 0,
    FirstName: self.FirstName || '',
    LastName: self.LastName || ''
   };

//Declare an ObservableArray for Storing the JSON Response
self.Users = ko.observableArray([]);

GetUser(12); //This is server side method.

function GetUser(userId) {
    //Ajax Call Get All Employee Records
    $.ajax({
        type: "GET",
        url: "/api/Users/" + userId,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            //alert("success");
            UserData = response.data.UserData;
            alert(UserData.FirstName); //This is showing me correct name.
            self.Users(response.data.UserData); //Put the response in ObservableArray
        },
        error: function (error) {
            alert(error.status);
        }
    });
    //Ends Here
}
}

ko.applyBindings(new UserViewModel());

以下是我的观点:

  <form class="form-horizontal" data-bind="with: UserData">
                           <div class="row">
                               <div class="control-group">
                                   <label class="control-label">First Name</label>
                                   <label class="control-label" data-bind="text: FirstName"></label>
                               </div>
                            </div>

                            <div class="row">
                              <div class="control-group">
                                   <label class="control-label">Last Name</label>
                                   <input type="text" placeholder="Last Name" data-bind="value: LastName">
                               </div>
                            </div>

2 个答案:

答案 0 :(得分:0)

您的问题是您正在尝试针对不可观察的属性进行双向数据绑定,因此当您更新它时,它不会通知UI。使您的用户成为可观察对象并将其设置为对象的实例或创建模型以从中派生属性。

function userModel(user) {
    var self = this;
    self.Id = ko.observable(user.Id);
    self.FirstName = ko.observable(user.FirstName);
    self.LastName = ko.observable(user.LastName);
}


var viewModel = function () {
    var self = this;    
    //The Object which stored data entered in the observables
    var UserData = ko.observable();
    GetUser(12);
    function GetUser(userId) {
        //Ajax Call Get All Employee Records
        $.ajax({
            type: "GET",
            url: "/api/Users/" + userId,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                //alert("success");
                UserData(new userModel(response.data.UserData));
                alert(UserData().FirstName()); //This is showing me correct name.
                self.Users.push(UserData()); //Put the response in ObservableArray
            },
            error: function (error) {
                alert(error.status);
            }
        });
    }
}
ko.applyBindings(new viewModel());

答案 1 :(得分:-1)

在这里,从以下小提琴中取出:: http://jsfiddle.net/9ZCBw/

你有一个拥有用户集合并具有该功能的容器模型(ajax被剥离以便在小提琴下使用)

function ViewModel () {
    var self = this;
    self.Users = ko.observableArray();

    self.GetUser = function (userId) {
        var UserData = {
            Id: userId,
            FirstName: 'Bob',
            LastName: 'Ross'
        };
        self.Users.push(new UserModel(UserData));
    }
}

在这里,您有用户的实例,然后可以绑定到您的模型......如图所示。