Click here to Skip to main content
12,299,109 members (49,038 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Javascript HTML5 , +
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.
Posted 27-Dec-12 1:11am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Well i think you must look into some JSON Serializers , for example Newtonsoft.NEt
JSON.NET etc.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100