A
A
artshelom2020-01-01 21:45:24
ASP.NET
artshelom, 2020-01-01 21:45:24

How to pass data to the controller??

Created an API controller that fails to pass data to:

[Route("api/User")]
        [ApiController]
        public class UserApiController : ControllerBase
        {
            [HttpPost("data")]
            public async Task<JsonResult> data(string dataOld, string dataNew)
            {
                return null;
            }
            [HttpPost("Settings")]
        public async Task<JsonResult> Settings([FromBody] JObject jObject)
        {
            return null;
        }

Empty data is passed to the data method, and the Settings method returns an error without executing the method.
HTML:
$.post(
        "/api/User/data",
        {
            dataOld: "param1",
            dataNew: "param2"
        },
        function (data) {
            alert(JSON.stringify(data));
        }
    );
    var books = [{
        name: "Чайка",
        price: 200,
    }, {
        name: "Война и мир",
        price: 230,
    }];
    $.ajax({
        url: '/api/User/Settings',
        type: 'POST',
        data: JSON.stringify(books),
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            alert('Данные отправлены');
        }
    });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2020-01-02
@artshelom

I think that the problem is
because in fact, with this line you are sending a string, not an object. Type mismatch, he does not see it.
If you want to accept an object in the method, you don't need to convert it to a string, you can just do it
. But something tells me that in your version there may be problems here as well. Why are you accepting a JObject?
Transform it later, accept it as an array
Better yet, type properly, and then do something like JObject.FromObject(books).
PS:
In such cases, it would be good to attach the text of the error to the question.

R
Roman, 2020-01-02
@yarosroman

1. You call the Password method in the first case.
2. In the parameters of the controller methods, there must be a data model that you pass, such as

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 built differently, the controller should have methods that are responsible for CRUD operations and, accordingly, are performed by different types of requests, GET - data selection, POST - adding data, PUT - Modification, DELETE - deletion. And one controller is responsible for working with only one data type, so if you have UsersController, then work only with the Users type,
4. Return IActionResult from the controller, The controller has several methods like Ok, BadRequest, NotFound, NoContent. Accordingly, they set the response status (200,404, etc.), which you will need to handle errors
5. Here is an example of a RestAPI controller - https://pastebin.com/PykpEGWP,those if your controller methods must send and receive only the User type and send only it from the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question