0

I want to send an array in a JSON string to an API and then handle the data. Despite creating a class and trying to let it map it I get it as null

This is my AJAX call (part of):

var ids = {
  rateplanIds: ['100', '200', '300']
};
$.ajax({
  url: g_appVirtualPath + "ApiFile.mvc/CheckMBB/",
  async: false,
  global: false,
  type: "POST",
  data: {
  ids: JSON.stringify(ids)
  }
})

This is my API (part of):

        public class RateplanIds
        {
            [DataMember]
            public string[] rateplanIds { get; set; }


        }


        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public JsonResult CheckMBB(RateplanIds ids) //ids = null
        {

        }

13
  • add contentType:"application/json" for your ajax request Commented May 3, 2019 at 9:23
  • @vikscool I have added dataType: "json" following the patern in other AJAX calls in the same project. Commented May 3, 2019 at 9:25
  • Then @FotisPapadamis try changing your data to what @Narendran has mentioned. Commented May 3, 2019 at 9:27
  • @vikscool after changing data ids is still null but it is associated with the class. Previously it was just null Commented May 3, 2019 at 9:29
  • why not string[] as a parameter in the controller Commented May 3, 2019 at 9:30

2 Answers 2

0

use this:

    [AcceptVerbs(HttpVerbs.Post), Authorize]
    public JsonResult CheckMBB([FromBody]RateplanIds ids) //ids = null
    {

    }

instead of:

    [AcceptVerbs(HttpVerbs.Post), Authorize]
    public JsonResult CheckMBB(RateplanIds ids) //ids = null
    {

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

Comments

0

The problem is that you have to many shells around your data. The data property of the AJAX request just has to be data: JSON.stringify(ids)

Note that every {} is a new Object

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.