Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Tadit Dash 330
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,345
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid