1

I am able to send a single complex type from ajax to controller however not able to pass a list, always comes up null. *** Code has been updated to show successful passing.

public class EntityAliasTest
{
  public int IDTEMP1 { get; set; }
  public string NAMETEMP1 { get; set; }
}
[HttpPost]
public IActionResult SaveEntityAliases([FromBody] 
List<EntityAliasTest> postData)
{
  var es = ModelState.SerializeErrors();
  return Json(new[] { postData, es });
}
var aliasList = new Array();

var o = { IDTEMP1 : 0, NAMETEMP1: 'test 0' };
aliasList.push(o);

o = { IDTEMP1 : 1, NAMETEMP1: 'test 1' };
aliasList.push(o);

o = { IDTEMP1 : 2, NAMETEMP1: 'test 2' };
aliasList.push(o);

var postData= JSON.stringify(aliasList);

$.ajax({
  url: "DataAdministration/EntityAlias/SaveEntityAliases",
  type: "POST",
  cache: false,
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  data: postData,
}).done(function (data) {
2
  • At first glance your code seems fine. Have you checked the console for errors or added any error handling code to see what the response from the request is? Commented Aug 18, 2017 at 18:15
  • I have updated the code to show it passing successfully. Thanks for the help everyone! Commented Aug 18, 2017 at 23:35

1 Answer 1

1

Based on your ajax call, you are sending a single object that contains a list. So your controller will also expect an object with that kind of structure.

Instead of this: var data = { postData: aliasList }; var data2send = JSON.stringify(data);

You can try: var data2send = JSON.stringify(aliasList);

You can use online json validator to check if you pass a valid json data. You can refer on the link below.

https://jsonlint.com/

Hope it will help.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated and validated the JSON, all is good with it, however still getting null IEnumerable<EntityAliasTest> postData in my controller? Any thoughts?
Made a few additional changes but this got me to right spot. Thanks! I marked a checked but said something about 15 checks needed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.