Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing A List Of Objects Into An ActionResult MVC Controller Method Using jQuery Ajax

possible duplicate Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

but my question is when I pass

var things = [
  {employee:'test',effectiveDate:'',expirationDate:'' },
  { employee:'test',effectiveDate:'',expirationDate:'' }
];

$.ajax({
 contentType: 'application/json',
 type: "POST",
 url: "/MyController/CheckMethod",
 dataType: "json",
 data: JSON.stringify(things),
 async: false,
 success: function (data) {

to an controller method which is a [HTTPPOPST] JsonResult then I'm getting value into my List<MYMODEL>

but when I take a controller method as 'ActionResult' then i'm getting null in List<MYMODEL>

why so any thing wrong?

like image 478
Neo Avatar asked Jan 10 '23 08:01

Neo


1 Answers

I think first of all your JSON should be strongly typed. and once it already strongly typed you need not use JSON.stringfy. instead go with,

data: {"things" : things},

and your controller should be like

public IActionResult ActionName(List<Model> things)
like image 69
Sarvagya Saxena Avatar answered Jan 11 '23 21:01

Sarvagya Saxena