0

I'm using ASP.NET Core MVC.

Javascript:

var currentSelection = [];

function selection_changed(selectedItems) {
    var dataGrid = $("#myContainer").dxDataGrid("instance");
    var data = selectedItems.selectedRowsData;

    if (data.length > 0) {
        currentSelection = data
            .map((value) => `${value.Name}`)
    } else {
        currentSelection = [];
    };
}

function create(e) {
    $.ajax({
        url: "@Url.Action("Register", "MyController")",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        type: 'POST',
        data: JSON.stringify(currentSelection),
        success: function (response) {
            showToast("success");
        },
        error: function (xhr, status, error) {
            showToast("error");
        }
    });
}

Unprocessed request looks like:

["Name1","Name2"]

C#

[HttpPost]
public virtual ActionResult Register(object currentSelection)

I have tried without JSON.stringify, setting

data: {currentSelection: JSON.stringify(currentSelection)}

tried adding this traditional: true.

I have also tried adding [FromBody] to controller, instead of object tried List<string> or string[].

Nothing is working. I'm getting HTTP 400 error every time. When I changed code to GET request without any data etc., everything is working, so no URL/communication issues.

Can someone help me with this?

4
  • @YongShun as i mentioned in the post - tried that, same result, http400 Commented Jun 27, 2024 at 12:48
  • Does this answer your question? Post array of strings to web API method Commented Jun 27, 2024 at 12:57
  • @YongShun Im getting same result after implementing solution from topic You've brought. Commented Jun 27, 2024 at 13:04
  • Is there any middleware for filter which might change the parameter inside the request? If not, have you tried to call this API via tools like postman? By the way, we might create a new .net 7/8 web api project and put the Post methed into it, then we can use the Swagger tool to see which kind of parameter we should pass to call this API. Commented Jun 28, 2024 at 6:58

2 Answers 2

0

Your problem is the same as was answered here: Post array of strings to web API method

Effectively, what you need to do is to turn your requests data into an object instead of just a string. Using what you have, I'd suggest adding the [FromBody] statement to your Register method, then trying something like this:

function create(e) {
$.ajax({
    url: "@Url.Action("Register", "MyController")",
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    type: 'POST',
    data: {"currentSelection": currentSelection},
    success: function (response) {
        showToast("success");
    },
    error: function (xhr, status, error) {
        showToast("error");
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Same result, HTTP400
Sounds like you have an issue with setting up your controller in general then. Try adding a simple GET endpoint that doesn't take any parameters, and work on that until you get a 200 response from it.
as i mentioned in my original post: GET request without any data etc. works totally fine.
At that point, I'd try creating a POST method that doesn't take parameters to ensure you can call a POST endpoint. It might also be helpful if you posted the entirety of the controller instead of just the method declaration line.
0

I noticed you declared as ASP.NET Core MVC so that I had a test with code below in ASP.NET Core 7 MVC application, which worked for me. I post a screenshot to prove it. Could you confirm whether you code use mock data to send the post request and get the desired result?

$("#btn2").click(function () { 
    var currentSelection = ["Name1", "Name2"];
    $.ajax({
        type: 'POST',
        url: 'Hello/Register',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(currentSelection),
        success: function (res) {
            console.info(res);
        }
    })
})


[HttpPost]
public virtual IActionResult Register([FromBody] List<string> data) {
    return Ok();
}

enter image description here

2 Comments

Thank You. I will try this solution, and leave comment whether it's working or not.
Please try to keep clarifications to comments under the question; questions about the question don't really belong in answers.

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.