javascript - How to pass a C# object to $.ajax -


i have simple javascript code in want object controller , parse in script, in script, don't have access property of object.

my object (in controller):

public class userdata {     public string firstname { get; set; }     public string lastname { get; set; }     public string state { get; set; }     public bool success { get; set; } } 

script:

function  load() {     var sender = {          userno : $("#userno").val(),     }     $.ajax({         type : "post",         url : "testgetuser",         data : sender,         success:function(data)             {                 if(1)                 {                     $("#section #result0").html(data.firstname);                     $("#section #result1").html(data.lastname);                     $("#section #result2").html(data.state);                     $("#section").slidedown().delay(1000).slideup();                 }             }     }); } 

controller:

[httppost] public userdata testgetuser(long userno) {     userdata result = new userdata();      result.firstname = session["firstname"].tostring();     result.lastname = session["lastname"].tostring();     result.state = session["country"].tostring();     result.success = true;      return result; } 

in controller, use this:

[httppost] public jsonresult testgetuser(long userno) {     userdata result = new userdata();      result.firstname = session["firstname"].tostring();     result.lastname = session["lastname"].tostring();     result.state = session["country"].tostring();     result.success = true;      return json(result); } 

it seems not using variable userno, better use request this:

public jsonresult testgetuser(/*remove parameter*/) {     userdata result = new userdata();      result.firstname = session["firstname"].tostring();     result.lastname = session["lastname"].tostring();     result.state = session["country"].tostring();     result.success = true;      return json(result, jsonrequestbehavior.allowget); } 

the javascript wants this:

function  load() {     $.ajax({         type : "get",         url : "testgetuser",         success:function(data) {             $("#section #result0").html(data.firstname);             $("#section #result1").html(data.lastname);             $("#section #result2").html(data.state);             $("#section").slidedown().delay(1000).slideup();         }     }); } 

Comments