淘汰赛 JS +发送到 MVC 3
我有以下代码,但是一旦将其提交到服务器,我将得到“未定义”字符串,而不是 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();
});
提前致谢
I've got the following code, but once it's submitted to the server I'm getting strings of "undefined" instead of null or empty. This is causing issues as I can't perform validation. Any ideas how to prevent this happening when using knockout.
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();
});
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是
JSON.stringify(viewModel)
而是ko.toJSON(viewModel)
,这将在进行 stringify 之前首先解开所有可观察量。这里有一些有用的文档:http://knockoutjs.com/documentation/json-data.htmlInstead of
JSON.stringify(viewModel)
doko.toJSON(viewModel)
, which will unwrap all of the observables first before doing a stringify. Some useful docs here: http://knockoutjs.com/documentation/json-data.html我认为首先你应该改变:
等级选择:ko.observable(Grades[0])
也许您想将 Grades 更改为 observableArray。我会把它放在评论中,但还不能。
I think first of all you should change:
gradeSelected: ko.observable(Grades[0])
And maybe you'd want to change grades to an observableArray. I'd place this in a comment, but can't yet.