我有以下代码,但一旦提交到服务器,我得到字符串“undefined”而不是null或空.这是导致问题,因为我无法执行验证.任何想法如何防止这种情况发生时使用淘汰赛.
var viewmodel = { userName: ko.observable(""),emailAddress: ko.observable(""),verifyEmailAddress: ko.observable(""),OptOut: ko.observable(true),Grades: ["Grade 1","Grade 2","Grade 3","Grade 4","Grade 5","Grade 6"],gradeSelected: ko.observable(["Grade 1"]) }; ko.applyBindings(viewmodel); $("#addUser").click(function (e) { $.ajax({ url: 'AddUser',dataType: 'json',data: JSON.stringify(viewmodel),type: 'POST',success: function (data) { $("#errorSection").text(data.Success).show(); } }); e.preventDefault(); });
提前致谢
解决方法
而不是JSON.stringify(viewmodel)do ko.toJSON(viewmodel),它将在执行stringify之前首先解开所有的可观察器.这里有一些有用的文档:
http://knockoutjs.com/documentation/json-data.html