I am trying to pass a collection of data to my C# controller via AJAX from JavaScript. The data being passed will not always be the same. There is no create/update/delete to the system happening at all this is purely a read operation.
My object looks like this:
values = {
Id: [SOME INT ID],
DB: [SOME DB ID],
Values: [{collection of values}]
}
This is my ajax call:
$.ajax({
url: "MYURL?" + encodeURIComponent(JSON.stringify(values)),
type: "GET",
success: function(data){
// do callback stuff
},
dataType: "json"
});
My controller is:
[HttpGet]
public ActionResult MyController(DataViewModel viewModel){
// Stuff and Things code
}
The data is not being populated in the controller in the viewModel at all. All the values are null. How can I pass the JSON data into the controller? Thank you in advance.
urlin your ajax method?$.ajax({ ... data: values })instead of stringifying them? I'm pretty sure jQuery will encode the parameters for you.DataViewModeland the{collection of values}code. @Pluto has the correct advice in the comment above.values.Valuesmight not be sent.