ASP.NET Core 2, jQuery POST data null

14,263

Solution 1

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)
{

}

Solution 2

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)
{

}
Share:
14,263

Related videos on Youtube

al.koval
Author by

al.koval

Updated on November 08, 2022

Comments

  • al.koval
    al.koval over 1 year

    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; 
    }
    
    • mrogal.ski
      mrogal.ski over 6 years
      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.
    • al.koval
      al.koval over 6 years
      This example works correctly in ASP.NET MVC. There's a difference between being processed by the server the POST method?
    • mrogal.ski
      mrogal.ski over 6 years
      It's just common sense that JSON is a different type than two string s so it's worth a shot.
    • naslund
      naslund over 6 years
      See this article on how model binding and json works in asp.net core: andrewlock.net/model-binding-json-posts-in-asp-net-core
    • al.koval
      al.koval over 6 years
      Added a solution to the question in the text.
    • Rudresh Bhatt
      Rudresh Bhatt over 6 years
      I was missing to add [FromBody]; Silly mistake, please check the same if anyone is facing the same issue.
  • mrogal.ski
    mrogal.ski over 6 years
    Your example is for GET method, not POST. You're sending parameters as a part of query string.
  • Rudresh Bhatt
    Rudresh Bhatt over 6 years
    I was missing to add [FromBody]; Silly mistake, please check the same if anyone is facing the same issue.
  • Elnoor
    Elnoor about 5 years
    In you example there more are than 1 [FromBody] attributes, which is wrong. Method can only have 1 parameter with [FromBody] attribute