KnockoutJS将对象数组发送到Asp.net页面 [英] KnockoutJS sending object array to Asp.net page
问题描述
我仍然是Knockout和Javascript的新手并且已经在网上搜索但是找不到我在asp.net页面中有以下javascript问题的解决方案。
我想要实现的是将客户端的对象数组发送到asp.net页面方法saveClients,如何在asp.net页面中的C#代码中接收对象数组?然后我如何将一个对象数组发送回javascript函数loadClients
这是我的html页面中的javascript
function client(_name,_age){
return {
name:ko.observable(_name),
age:ko.observable(_age)
// remove:function(){
// vm.clients.remove(this);
// }
}
};
var vm = {
clients:ko.observableArray(),
addClient: function (){
this .clients.push(新客户端( document .getElementById( inputName)。value, document .getElementById( inputAge跨度>)值))。
},
saveClientsEvent: function (){
saveClients();
},
loadClientsEvent: function (){
loadClients();
}
}
function saveClients(){
< span class =code-keyword> var d = {
clients:vm.clients()
}
$ .ajax({
类型: POST,
url: jumpStart.aspx / saveClients,
data: JSON .stringify( d),
dataType: JSON,
contentType: application / json,
成功: function (result){
alert( success);
},
错误: function (结果){
alert( 错误);
}
});
}
function loadClients(){
$。 ajax({
类型: POST,
url: jumpStart.aspx / loadClients,
dataType: JSON,
contentType: application / json; charset = utf-8,
成功:功能(结果){
// alert(成功);
alert(result.d.age);
},
错误: functi on (结果){
alert( error);
}
});
}
$( document )。ready( function (){
ko.applyBindings(vm);
});
这是我的asp.net页面背后的代码。
[System.Web.Services.WebService]
public partial class jumpStart :System.Web.UI.Page
{
protected void Page_Load(< span class =code-keyword> object sender,EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string saveClients ( object [] clients)
{
return Hello ;
}
[System.Web.Services.WebMethod]
public static client [] loadClients()
{
client [] clients = new client [ 2 跨度>];
clients [ 0 ] = new client {name = Freddie,age = 28\" 跨度>};
clients [ 1 ] = new client {name = Fanie,age = 28};
返回客户;
}
public class client
{
public string name { get ; set ; }
public string age { get 跨度>; set ; }
}
}
将一个字符串或int发送到页面并返回很简单,但是如何发送和解析一个对象数组呢?
任何帮助都将不胜感激。
.ajax({
type: POST,
url: jumpStart.aspx / saveClients,
数据: JSON .stringify(d),
dataType: JSON,
contentType: application / json ,
成功:功能(结果){
alert( success);
},
错误: function (结果){
alert( 错误);
}
});
}
function loadClients(){
.ajax({
type: POST,
url:< span class =code-string> jumpStart.aspx / loadClients,
dataType: JSON,
contentType: application / json; charset = utf-8,
成功:功能(结果){
// alert(成功);
alert(result.d.age);
},
错误:功能ion (结果){
alert( error);
}
});
}
( document )。ready(功能(){
ko.applyBindings(vm);
});
这是我的asp.net页面背后的代码。
[System.Web.Services。 WebService]
public partial class jumpStart:System.Web.UI.Page
{
protected void Page_Load ( object sender,EventArgs e)
{
}
[System.Web.Services。 WebMethod]
public static string saveClients( object [] clients)
{
return Hello;
}
[System.Web.Services.WebMethod]
public static client [] loadClients()
{
client [] clients = new client [ 2 跨度>];
clients [ 0 ] = new client {name = Freddie,age = 28\" 跨度>};
clients [ 1 ] = new client {name = Fanie,age = 28};
返回客户;
}
public class client
{
public string name { get ; set ; }
public string age { get 跨度>; set ; }
}
}
将一个字符串或int发送到页面并返回很简单,但是如何发送和解析一个对象数组呢?
任何帮助都将不胜感激。
I''m still new to Knockout and Javascript and have searched the web but can not find a solution for the problem that I have the following javascript in an asp.net page.
What I am trying to achieve is to send a object array of client to the asp.net page method "saveClients",how do I recieve the object array in my C# code in the asp.net page ? And then how do I send an object array back to the javascript function "loadClients"
THis is the javascript inside my html page
function client(_name, _age) {
return {
name: ko.observable(_name),
age: ko.observable(_age)
// remove: function () {
// vm.clients.remove(this);
// }
}
};
var vm = {
clients: ko.observableArray(),
addClient: function () {
this.clients.push(new client(document.getElementById("inputName").value, document.getElementById("inputAge").value));
},
saveClientsEvent: function () {
saveClients();
},
loadClientsEvent: function(){
loadClients();
}
}
function saveClients() {
var d = {
clients: vm.clients()
}
$.ajax({
type: "POST",
url: "jumpStart.aspx/saveClients",
data: JSON.stringify(d),
dataType: "JSON",
contentType: "application/json",
success: function (result) {
alert("success");
},
error: function (result) {
alert("error");
}
});
}
function loadClients() {
$.ajax({
type: "POST",
url: "jumpStart.aspx/loadClients",
dataType: "JSON",
contentType: "application/json; charset=utf-8",
success: function (result) {
// alert("success");
alert(result.d.age);
},
error: function (result) {
alert("error");
}
});
}
$(document).ready(function () {
ko.applyBindings(vm);
});
This is the code behind in my asp.net page.
[System.Web.Services.WebService]
public partial class jumpStart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string saveClients(object[] clients)
{
return "Hello";
}
[System.Web.Services.WebMethod]
public static client[] loadClients()
{
client[] clients = new client[2];
clients[0] = new client{name="Freddie",age="28"};
clients[1] = new client { name = "Fanie", age = "28" };
return clients;
}
public class client
{
public string name { get; set; }
public string age { get; set; }
}
}
Sending a string or int to the page and back is easy but how do you send and parse an object array ?
Any help would be appreciated.
.ajax({ type: "POST", url: "jumpStart.aspx/saveClients", data: JSON.stringify(d), dataType: "JSON", contentType: "application/json", success: function (result) { alert("success"); }, error: function (result) { alert("error"); } }); } function loadClients() {
.ajax({ type: "POST", url: "jumpStart.aspx/loadClients", dataType: "JSON", contentType: "application/json; charset=utf-8", success: function (result) { // alert("success"); alert(result.d.age); }, error: function (result) { alert("error"); } }); }
(document).ready(function () { ko.applyBindings(vm); });
This is the code behind in my asp.net page.
[System.Web.Services.WebService] public partial class jumpStart : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string saveClients(object[] clients) { return "Hello"; } [System.Web.Services.WebMethod] public static client[] loadClients() { client[] clients = new client[2]; clients[0] = new client{name="Freddie",age="28"}; clients[1] = new client { name = "Fanie", age = "28" }; return clients; } public class client { public string name { get; set; } public string age { get; set; } } }
Sending a string or int to the page and back is easy but how do you send and parse an object array ?
Any help would be appreciated.
这篇关于KnockoutJS将对象数组发送到Asp.net页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!