14

I use jQuery and send data with the POST method. But in the server method the values are not coming. What could be the error?

client

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "./AddTag",
    dataType: "json",
    data: "{'parentId':42,'tagName':'isTagName'}",
    success: function (response) {
        // ...
    }
});

server

[HttpPost]
public JObject AddTag(int parentId, string tagName)
{
    dynamic answer = new JObject();
    List<LogRecord> logs = new List<LogRecord>();
    answer.added = fStorage.Tags.AddTag(parentId, tagName, logs);
    return answer;
}

Brackpoint

Chrome

fixed Thank you all very much. I understood my mistake. I fixed the client and server code for this:

let tag = {
        "Id": 0,
        "ParentId": 42,
        "Name": isTagName
    };
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "./AddTag",
    dataType: "json",
    data: JSON.stringify(tag),
    success: function (response) {
        // ...
    }
});

server

    [HttpPost]
    public JObject AddTag([FromBody] Tag tag)
    {
        dynamic answer = new JObject();
        List<LogRecord> logs = new List<LogRecord>();

        answer.added = fStorage.Tags.AddTag(tag.ParentId, tag.Name, logs);
        answer.logs = Json(logs);

        return answer;
    }

The class has added

public class Tag
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public List<Tag> ChildsList { get; set; }
    [NonSerialized]
    public Tag ParrentTag; 
}
6
  • 1
    You're sending json instead of query string, change your data to be "parentId=42&tagName=isTagName" and dataType to pplication/x-www-form-urlencoded and try again. Commented Sep 8, 2017 at 10:00
  • This example works correctly in ASP.NET MVC. There's a difference between being processed by the server the POST method? Commented Sep 8, 2017 at 10:03
  • It's just common sense that JSON is a different type than two string s so it's worth a shot. Commented Sep 8, 2017 at 10:06
  • See this article on how model binding and json works in asp.net core: andrewlock.net/model-binding-json-posts-in-asp-net-core Commented Sep 8, 2017 at 10:11
  • Added a solution to the question in the text. Commented Sep 8, 2017 at 10:23

3 Answers 3

13

Try extracting your params into a separate DTO class and do it like that:

public class ParentDTO 
{
 public int parentId{get; set;}
 public string tagName{ get; set;}
}

[HttpPost]
public JObject AddTag([FromBody] ParentDTO parent)
{

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

1 Comment

I was missing to add [FromBody]; Silly mistake, please check the same if anyone is facing the same issue.
5

Use [FromBody] before the param. It's check and Get the Property value in body otherwise it's check the Url Querystring.

Example:

[HttpPost]
public JObject AddTag([FromBody] int parentid,[FromBody]string tagname)
{

}

[HttpPost]
public JObject AddTag([FromBody] {ModelName} parent)
{

}

1 Comment

In you example there more are than 1 [FromBody] attributes, which is wrong. Method can only have 1 parameter with [FromBody] attribute
-5

Change your ajax to this

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "./AddTag?parentId="+42+"&tagName="+'isTagName',
dataType: "json",
success: function (response) {
    // ...
}

});

1 Comment

Your example is for GET method, not POST. You're sending parameters as a part of query string.

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.