c# - Passing values to webmethod using Json -


i having trouble sending values webmethod using json.

public class order {     public string item { get; set; }     public string color { get; set; }     public string qty { get; set; }     public string size { get; set; } }  [webmethod] public static string sendorder(string name, string branch, string jobfunction, string requestreason, string logo, string json) {     var orders = jsonconvert.deserializeobject<list<order>>(json); } 

here ajax call:

$('#submitorder').on('click', function (e) {      var table = $('#orders-table').tabletojson({         ignorecolumns: [4]     });      var obj = {         name: $('#fullnametbx').val(),         branch: $('#address').val(),         jobfunction: $('#jobfunction').val(),         requestreason: $('#requestreason').val(),         logo: $('#logoonshirt').val(),         orders: table     };     alert(json.stringify(obj));     $.ajax({         type: "post",         url: "apparel.aspx/sendorder",         data: { data: json.stringify(obj) },         contenttype: "application/json",         datatype: "json",         success: function (msg) {             alert(msg);         }     }); }); 

here fiddler:

{    'name': 'roger rabbit',    'branch': 'phx',    'jobfunction': 'service tech',   'requestreason': 'new hire',   'logo': 'nike',   'json': '[     {       "item":"port authority women's jacket #l790 - black",       "size":"x-small",       "color":"black",       "quantity":"3"     },     {       "item":"port authority® long sleeve easy care shirt",       "size":"3xl-tall",       "color":"black",       "quantity":"4"     }]' } 

the issue have of basic parameters strings last parameter called "json" , list of orders in html table , call stringify on table think coming bad result.

how can change ajax call pass correct values correct parameters?

change web method accept object

[webmethod] public static string sendorder(ienumerable<order> order) {     //no need deserialize json } 

you can lose json property on order class too.

also pop datatype: "json" jquery ajax call.

if need retain parameters create class composed of these , order class, pull them out inside webmethod.

public class orderviewmodel {     public ienumerable<order> orders { get; set; }      public string name { get; set; }      // ... etc ... // } 

change ajax rather concatenating string yourself:

var obj = {     "name" : $('#fullnametbx').val(),     "branch" : $('#address').val(),     "jobfunction" : $('#jobfunction').val()     // ... etc ... // };  $.ajax({     type: "post",     url: "apparel.aspx/sendorder",     data: json.stringify(obj),     datatype: 'json',     contenttype: "application/json",     success: function (msg) {         alert(msg);     } }); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -