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)
{
}
Related videos on Youtube
Author by
al.koval
Updated on November 08, 2022Comments
-
al.koval over 1 year
I use
jQuery
and send data with thePOST
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; }
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 over 6 yearsYou're sending json instead of query string, change your
data
to be "parentId=42&tagName=isTagName" anddataType
topplication/x-www-form-urlencoded
and try again. -
al.koval over 6 yearsThis example works correctly in ASP.NET MVC. There's a difference between being processed by the server the POST method?
-
mrogal.ski over 6 yearsIt's just common sense that
JSON
is a different type than twostring
s so it's worth a shot. -
naslund over 6 yearsSee 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 over 6 yearsAdded a solution to the question in the text.
-
Rudresh Bhatt over 6 yearsI was missing to add [FromBody]; Silly mistake, please check the same if anyone is facing the same issue.
-
-
mrogal.ski over 6 yearsYour example is for GET method, not POST. You're sending parameters as a part of query string.
-
Rudresh Bhatt over 6 yearsI was missing to add [FromBody]; Silly mistake, please check the same if anyone is facing the same issue.
-
Elnoor about 5 yearsIn you example there more are than 1
[FromBody]
attributes, which is wrong. Method can only have 1 parameter with[FromBody]
attribute