views:

48

answers:

3

what i mean by that is:

i have a class called Customer:

public class Customer
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; } 
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; } 
    }
}

how do i instantitate the class "Customer" on the client code and add the data and post it? (not sure if this is possible)

here is my client code:

 var customer = {
                  "firstName": escape($('#txtFirstName').val()),
                  "lastName": escape($('#txtLastName').val())
                };

var jsonText = JSON.stringify({ customer: customer });

$.ajax({
    type: "POST",
    url: "VisitorWS.asmx/AddCustomer",
    data: jsonText,
    //data: JSON.stringify(params),
    contentType: "application/json; charset=utf-8",
    ...........
});
A: 

if it is in mvc check this post by RM

XGreen
its a web form..
Abu Hamzah
A: 

The act of taking your C# class and "sending" it between the client and server is called Serialization. You could, if you want, define a javascript object that maps member-to-member to your Customer class. That's usually not necessary, however. You can use the [Serializable] attribute to "convert" your object to a textual representation in XML or JSON, then send that to the client for processing in that format. There are classes built into .NET that can then "deserialize" the textual format back into an instantiated object on the server.

Dave Swersky
+1  A: 

Like Dave said you have to serialize the object between the two layers just as you do passing your object from your business layer to your database, etc. In the example you are showing above you are passing a JSON object to an old fashion ASMX web service. So make sure it can deserialize the JSON object to your C# class.

I have a series of blog post on binding WCF with jQuery using JSON.

http://professionalaspnet.com/archive/tags/WCF/default.aspx

You can download my latest sample code:

http://professionalaspnet.com/WCFJQuery.zip

They recorded my Philly Code Camp presentation this weekend, you can watch that, along with 9 other talks here:

http://codecamp.phillydotnet.org/2010-1/Lists/Sessions/MSDN.aspx

Chris Love
thanks Chris i will look into it.along the lines you have said old fashion asmx web service, should i be using WCF instead of web services? would you please elaborate little bit more :)thanks again.
Abu Hamzah
Thanks Chris for links.
Abu Hamzah
While WCF is percieved to be more complicated, and it can be don't get me wrong, you have a much more robust infrastructure in place. You have all sort so protocols that come out of the box, security infrastructure, etc.
Chris Love