I have done an udemy Tutorial for MVC in dotnet5 and try to implement my own project on this base. I have built a form with a body like this:
<form id="genreForm" autocomplete="off" novalidate="novalidate">
<div class="">
Genre
</div>
<div class="container closed">lorem Ipsum</div>
<div>
<input type="text" id="newGenreName" />
<input type="text" id="newGenreDescription" />
<button type="button" id="btnSubmit" class="btn btn-success" onclick="onGenreAddForm();">Genre hinzufügen</button>
</div>
</form>
And an ajax request like this:
function onGenreAddForm() {
var requestData = {
GenreName: $("#newGenreName").val(),
GenreDescription: $("#newGenreDescription").val()
};
$.ajax({
url: routeURL + '/api/Event/AddUpdateGenre',
type: 'POST',
data: JSON.stringify(requestData),
contentType: 'application/json',
success: function (response) {
},
error: function (xhr) {
$.notify("Error", "error");
}
});
}
which routes to an API Controler looking like this:
[HttpPost]
[Route("AddUpdateGenre")]
public IActionResult AddUpdateGenre(ManagementVM data)
{
doSthWithData(data);
}
while the ManagementVM has members like
public string GenreName { get; set; }
public string GenreDescription { get; set; }
Now, when i fire the button, the js fills the requestData with the right values an the right keys, but when i inspect ManagementVM data in the APIController, it is filled with nulls. Can anyone tell me, where is my fault? I followed the same steps described in the tutorial.
Thanks a lot!
solved by adding [FromBody] to the Param in API:
public IActionResult AddUpdateGenre([FromBody] ManagementVM data)