0

I call a mvc controller using ajax using :

 var ajaxOptions = { url: url, type: 'POST', contentType: 'application/json', 
                     data: JSON.stringify(data) };

I send the following JSON in data

data = "{"TagList":["AA","BB","CCC","DDDD"]}"

In my controller the following method is called

[HttpPost]
public async Task<JsonResult> Update(TagItem tagItem)

and I get TagItem.TagList = null

public class TagItem
{
    public List<string> TagList { get; set; }
}

2 Answers 2

4

You are missing the tagItem wrapper in JSON.

data = {"tagItem":{"TagList":["AA","BB","CCC","DDDD"]}};

var ajaxOptions = 
             { url: url, 
               type: 'POST', 
               contentType: 'application/json; charset=utf-8', 
               data: JSON.stringify(data) 
              };

Try this for instance:-

  var data = { "tagItem": { "TagList": ["AA", "BB", "CCC", "DDDD"]} };

    $.ajax({
        type: 'POST',
        url: "home/test",
        data: JSON.stringify(data),
        contentType: 'application/json; charset=utf-8'
    });
Sign up to request clarification or add additional context in comments.

Comments

2

Try this with contentType:

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: url,
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8'
});

Added:

As @PSL found OP lost tagItem, after wrapping current data with it, code must work.

1 Comment

Looking at his issue it seems more like he is missing the wrapper.

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.