jquery - $.Ajax post array to Web Api 2 not working for me -


data model

public class foo {     public string id { get; set; }     public string title { get; set; } } 

web api 2 controller

[route("api/updatefoo")] public ihttpactionresult updatefoo(list<foo> foos) {  } 

js

// here need create foodata list of foo   var foodata = []; foodata.push({ title: "title1" }); foodata.push({ title: "title2" });  $.ajax({     method: "post",     url: "api/updatefoo",     data: foodata     }).done(function (result) {         //     }).fail(function(xhr) {         alert(xhr.responsetext);     }); 

what missing here? in fiddler looks i've sending foodata right not recieved in web api controller, being able enter the method breakpoint value in empty list

create model contain list

public class listoffoos {     public list<foo> foos {get; set;}      public listoffoos()     {         foos = new list<foo>();     } } 

then controller should this:

[route("api/updatefoo")] public ihttpactionresult updatefoo(listoffoos listoffoos) {  } 

now if pass in json this, should able list in controller:

{   "foos" : [     { "id": 1, "title" : "f1" },      { "id": 2, "title" : "f2" }   ] } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -