asp.net mvc - dont Post JSON array to mvc controller -


i'm trying post json mvc controller through ajax. use code not post.

$(document).ready(function(){     $(".btn-success").click(function(e){         var urdata = { city: 'moscow', age: 25 };         $.ajax({             url: "/category/create/",               type: "post",             datatype: "json",             traditional: true,             contenttype : "application/json",             data: urdata,             success: function(maindta) {                 alert(maindta);             },             error: function(jqxhr, textstatus){             }         });         e.preventdefault(); //stop default action     }); }); 

this controller action

[httppost] public virtual jsonresult create(list<object> urdata){  } 

your posting object 2 properties need model 2 properties

public class mymodel {     public string city { get; set; }     public int age { get; set; } } 

and method needs changed to

public jsonresult create(mymodel model) {    .... // model populated values sent    return json(...);` } 

and need remove following options ajax method

contenttype : "application/json", traditional: true, 

alternatively, method can be

public jsonresult create(string city, int age) 

but lose benefits of model validation

side note: use url.action() method ensure url's correctly generated

url: '@url.action("create", "category")',   

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -