In my project (ASP.NET Core 2.2. MVC) I'm trying to do an ajax post with additional parameters to the view model. The form on the page has an id='frmMain'.
My js looks like this:
//post
var data = {
model: $("#frmMain").serialize(),
passtest: 'test'
};
$.ajax({
type: 'post',
url: url,
data: JSON.stringify(data),
dataType: 'json',
success: function () {
//alert('form was submitted');
}
}).done(function (result) {
if (result.status === "success") {
//some code here
}
} else {
//some code here
}
});
My controller actions looks like this:
public IActionResult DoSomething(MyModel model, string passtest)
{
//some action code here
}
Now, the post works, but in my controller action only "model" is filled with data. Variable "passtest" is null. Also there is no "passtest" in the "model".
What am I doing wrong?
Note: I found a workaround with filling hidden fields (and adding them to the view model), but plainly passing multiple parameters in the post itself seems more practical...