Asp.net controller recieving null

My ASP.net controller is receiving null instead of passed in parameters ๐Ÿ™
Js function:

function SendFormToController() {
            var username = document.getElementById("UsernameField").value;
            var email = document.getElementById("EmailField").value;
            var password = document.getElementById("PasswordField").value;

            var SendJson = {
                Username: username,
                Email: email,
                Password: password
            };
            console.log(JSON.stringify(SendJson));
            $.ajax({
                type: "POST",
                data: JSON.stringify(SendJson),
                url: "Register/Register",
                contentType: "application/json"
            });
        }

Data is present when I console log it. But in the controller, I get โ€“ https://prnt.sc/u2mpa6
And it is for every field here

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & Iโ€™ll get back to you as soon as possible.

Method 1

First; Did you add the [HttpPost] attribute on top of your controller method?

Second; If you submit it in โ€˜querystringโ€™ format: Username=xx&Password=yy&โ€ฆ and use (HttpGet). Does that work?

If you need to do a POST (and not want to use GET) you can create an object with all your current arguments and use the [FromBody] attribute:

[Route("Register")]
[HttpPost]
public ResultData Register([FromBody] RegisterRequest data) {
   //Your logic...
}

And client (JavaScript) side:

let url = 'http://...';
$.post(url, { Username: name, Password: pass, What: ever  }, function (result) {
    //Do some nice stuff
})
.fail(function (error) {
    //Oh no! Show error.statusText
});


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x