c# - How do I post a complex object to an MVC 4 controller via Ajax? -


i have object called drawdie following properties:

id plantid : int qtyonhand : int sizeus : double sizemetric : double casesize : string style : string 

i have object called dieorder following properties:

id : int drawdie : drawdie drawdieid : int plantid : int purchaseorder : string qty : int 

i post object mvc controller via ajax request. prefer not use 3rd party library. trying post this:

        var dieorder = new object();         var drawdie = new object();          dieorder.qty = $("#qty").val();         dieorder.drawdieid = $("#drawdieid").val();           drawdie.casesize = $("#drawdie_casesize").val();         drawdie.qty = $("#qty").val();                   drawdie.id = dieid;         drawdie.qtyonhand = 0;         drawdie.sizeus = $("#drawdie_sizeus");         drawdie.sizemetric = $("#drawdie_sizemetric");         drawdie.plantid = $("#plantid").val();         drawdie.istransfer = "n";          dieorder.plantid = $("#plantid").val();         dieorder.orderedby = $("#orderedby").val();         dieorder.purchaseorder = $("#purchaseorder").val();         dieorder.drawdie = drawdie;           $.ajax({             url: "order/orddie",             type: 'post',             contenttype: 'application/json; charset=utf-8',             data: json.stringify(dieorder),             success: handledata           });      }); 

i have 2 questions. one, approach way off or okay ? getting uncaughttype error: converting circular structure json. how restructure request not such error? unsure of why getting error.

thanks

update:

 drawdie.sizeus = $("#drawdie_sizeus").val();  drawdie.sizemetric = $("#drawdie_sizemetric").val(); 

i missing .val()

the problem you're having controller interpreting of contents of dieorder object arguments controller action.

if controller action looks this...

[httppost] public void orddie(orderdieviewmodel model) {     // operations } 

...then data stringifying should this:

{     'model': dieorder } 

...so, you're missing step prior making $.ajax call:

var drawdie = {     // properties... };  var dieorder = {     // properties...     'drawdie': drawdie };  var data = {     'model': dieorder };  $.ajax({     // spec object here. }); 

in short? data object's contents must match argument list of action calling. means if pass model json action, data object must have 1 member: javascript representation of model object.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -