How to pass data to controller??

Created an API controller, in which it is impossible to transfer data:
[Route("api/User")]
[ApiController]
 public class UserApiController : ControllerBase
{
[HttpPost("data")]
 public async Task<JsonResult> data(string dataOld, dataNew string)
{
 return null;
}
[HttpPost("Settings")]
 public async Task<JsonResult> Settings([FromBody] JObject jObject)
{
 return null;
 }


Data is transmitted in the method blank data, and the Settings returns an error without performing the method.
HTML:
$.post(
"/api/User/data",
{
 dataOld: "param1",
 dataNew: "param2"
},
 function (data) {
alert(JSON.stringify(data));
}
);
 var books = [{
 name: "Seagull",
 price: 200,
 }, {
 name: "War and peace",
 price: 230,
}];
$.ajax({
 url: '/api/User/Settings',
 type: 'POST',
 data: JSON.stringify(books),
 contentType: "application/json;charset=utf-8",
 success: function (data) {
 alert('Data sent');
}
 });
April 4th 20 at 00:30
2 answers
April 4th 20 at 00:32
Solution
I think that's the problem
JSON.stringify(books)
because in fact this line You are sending a string, not an object. A mismatch of the type that he sees.
If you want the method to accept the object, it is not necessary to the string to convert, you can just
data: books,
But something tells me that in Your version there might be problems. Why would You take the JObject?
Convert it then, accept it as it is an array
public async Task<JsonResult> Settings(object[] books)

Better yet, typeservice as it should be, and then do something like JObject.FromObject(books).

PS:
method Settings and returns an error without performing the method.

In such cases, it would be good if the error text to the question to make.
April 4th 20 at 00:34
1. You call the Password method in the first case.
2. Method parameters of controllers must be the data model that pass, like
class Book
{
 public string Name {get; set;}
 public int Price {get; set;}
}

 public async Task<IActionResult> Settings([FromBody] Book[] books)
{
return Ok()
}

3. RestApi is not built this way, the controller needs to be methods responsible for CRUD operations and respectively execute different types of queries, GET - fetch data, POST - adding of the data PUT - Modify, DELETE - DELETE. And one controller is responsible for only one type of data, those if you have a UsersController, the work goes only with the type of Users,
4. Return from IActionResult controller, the controller has several methods like Ok, BadRequest, NotFound, NoContent. Accordingly, they set the response status (200,404, etc.) that you need for error handling
5. Here's an example controller RestAPI - https://pastebin.com/PykpEGWP, if those are your methods of controller needs to send and receive only the type of User and client only send it.

Find more questions by tags ASP.NETC#