Json Data between UI and handler

Below are the code snippet to JavaScript and C# handler code. Objective is to know how to keep the property name of the classes in JavaScript and C#.
1. Create a javascript object in .js file
function jsonmenuitem(value,onclick) {
    this.value = value;
    this.onclick = onclick;
}

myjsonmenuitem = new jsonmenuitem("value","onclick");
2. Convert javascript object to Json string using a tool JSON.stringify
JSON.stringify(myjsonmenuitem);
3.  Pass it as 3rd parameter in DoAjaxCall() function
// Below code calls a function which does a ajax call.
DoAjaxCall("?method=Insert"
+"&callbackmethod="
+"InsertRadiographicWeldSucess&", "json",
JSON.stringify(myjsonmenuitem ));
4.Now in Server side handler class just De-serialize it to C# object
public string Insert(HttpContext context)
{
    System.Web.Script.Serialization
    .JavaScriptSerializer jSearializer
    = new System.Web.Script.Serialization
         .JavaScriptSerializer();

  jsonmenuitem_P = new jsonmenuitem();
   using (var reader
       = new StreamReader(
           context.Request.InputStream))
      {
          string jsonValue = reader.ReadToEnd();
          var js = new JavaScriptSerializer();
          _P = js.Deserialize<jsonmenuitem>
                  (jsonValue);
      }
}
This post is getting too long. Read the next part of the post here.
Advertisements

One thought on “Json Data between UI and handler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s