0

I know there are multiple threads around this issue, but I still can't figure mine out. Can someone please help me figure out why my classObject always has null value? I feel like I've tried everything by now.

My class:

public class ClassAB
{
 [Required]
 [MaxLength(100)]
 [DataType(DataType.Text)]
 public string A{ get; set; }
 [Required]
 [MaxLength(100)]
 [DataType(DataType.MultilineText)]
 public string B{ get; set; }
}

My home controller:

 [HttpPost]
 public ActionResult MyMethod(ClassAB classObject)
 {}

and my Javacript call

let data = { 
 "A": "A",
 "B": "B" 
}
await fetch(`https://localhost:44359/Home/MyMethod/`, {
 method: "POST",
 body: JSON.stringify(data),
 contentType:"application/json", 
 success: (result)=>{
 console.log(result)
 },
 failure: (result) => {
 alert(result)
 }
 });
asked Mar 20, 2020 at 9:21
0

3 Answers 3

1

Found the issue. My contentType should have been in header. Modifying request to

await fetch(`https://localhost:44359/Home/MyMethod/`, {
 method: "POST",
 body: JSON.stringify(data),
 headers: {
 'Content-Type': 'application/json'
 },
 success: (result)=>{
 console.log(result)
 },
 failure: (result) => {
 alert(result)
 }
 });

fixed the issue

answered Mar 20, 2020 at 10:37
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

 var data = [{A: 'A',B:'B'}]; 
 await fetch(`https://localhost:44359/Home/MyMethod/`, {
 method: "POST",
 body: JSON.stringify(data),
 contentType:"application/json", 
 success: (result)=>{
 console.log(result)
 },
 failure: (result) => {
 alert(result)
 }
 });
 [HttpPost]
 public ActionResult MyMethod(List<ClassAB > classObject)
 {}
answered Mar 20, 2020 at 9:43

5 Comments

I still get null value
did you try [FromBody]
I did. It didn't help neither
what happening if you pass a string
Changed contentType to text/html; charset=UTF-8, passed string in body, and still got null
0

WebAPI won't know to model bind an object like that. See https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1

Try using the [FromBody] attribute

[HttpPost]
public ActionResult MyMethod([FromBody] ClassAB classObject)
{}

When combining this with a proper javascript post this will work, see image.

enter image description here

Sample js

<script>
 var xhr = new XMLHttpRequest();
 var url = "https://localhost:5001/api/default/MyMethod";
 xhr.open("POST", url, true);
 xhr.setRequestHeader("Content-Type", "application/json");
 xhr.onreadystatechange = function () {
 if (xhr.readyState === 4 && xhr.status === 200) {
 var json = JSON.parse(xhr.responseText);
 console.log(json.email + ", " + json.password);
 }
 };
 var data = JSON.stringify({ "A": "A", "B": "B" });
 xhr.send(data);
</script>
answered Mar 20, 2020 at 9:33

1 Comment

Unfortunately it didn't help. Also, when I temporarily change type in controller, as in MyMethod(string classObject) I still get null

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.