Started by looking at a similar thread, but the Solution there didn't work for me.
Passing multiple objects to my controller
Here is my javascript onclick event:
var test = function () {
var vm = {
"IsNew": true,
"SelectedId": 1,
"SelectedCode": null
};
var idk= JSON.stringify(vm);
$.ajax({
url: $("base").attr("href") + "Controller/ComplexityTest",
method: "POST",
contentType: 'application/json',
data: idk,
success: function (msg) {
alert('success')
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error: " + textStatus + ", " + errorThrown);
}
});
};
On my Controller, I have this endpoint:
[HttpPost]
public async Task<IActionResult> ComplexityTest([FromBody] MyViewModel vm, long categoryId, long personId, DateTime startDate)
{
//...do work
}
For simplicity, let's say this is my Model:
public class MyViewModel
{
public bool IsNew {get; set;}
public int SelectedId {get; set;}
public string SelectedCode {get; set;}
}
On running this, I'm able to see that vm is populated correctly. The issue is that I'm also trying to populate some additional primitives to be sent along with MyViewModel.
I've tried adding the additional fields inside my vm initializer, and stringifying the whole thing.
I've tried setting data to { "vm": idk, "categoryId": 1, etc... }.
Everything I've tried so far has caused me to not only get the new parameters, but to lose my original vm object as well.
How do I go about passing an object (class) and multiple primitives to a POST method? Do I just need to create a new DTO object containing the additional parameters, and send that instead?