0

The jQuery code is :

var props = [
    { "Name": "firstName", "Value": "firstValue" }, 
    { "Name": "secondName", "Value": "secondValue" }
];

$.ajax({
     url: '/myURL',
     contentType: "application/json",
     async: true,
     type: "POST",
     data: JSON.stringify(props),
     error: function (jqXHR, textStatus, errorThrown) {
         console.log("FAIL: " + errorThrown);
     },
     success: function (data, textStatus, jqXHR) {
         console.log("SUCCESS!");
     }
});

The ASP.NET MVC controller

[HttpPost]
public async Task<ActionResult> Test(string myValue)
{
    return Json("something");
}

I hit the controller but myValue is null all the time.

Any idea ?

Thanks,

1
  • I don't hit the controller anymore and I get error 500. I checked with Fiddler for the request, json tab I have : JSON=myValue Commented Jul 26, 2022 at 9:40

1 Answer 1

1

Modify your API action to expect to receive the input of List<Prop> type.

[HttpPost]
public async Task<ActionResult> Test(List<Prop> myValue)
{
    return Json("something");
}
public class Prop
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Demo

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. But I'd like than the controller receive a string for compatibility reason with another stuff.
I doubt that it is possible to do. Perhaps I think you need to read from Request.InputStream. Refer to: MVC controller : get JSON object from HTTP body?
receive a string then you need to tell it you're sending a string - at the moment, you're telling it you're sending JSON (which, yes, is a string, but it's handled as json and converted to an object server-side before passing to your Action). Remove contentType: "application/json",

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.