As I mentioned in title I’m trying to send an array of view models back to a controller through an Ajax request.
I’ll use some dummy values and names:
MyViewModel.cs
int VM_id1;
int VM_id2;
string VM_str1;
string VM_str2;
Controller
// here I get 0 and null values
public ActionResult MyPostController(List<MyViewModel> vm) { ... }
View with ajax
In my view I have a form with fields from above, and a preview window where every time I submit form, those values are added there(not posting to controller yet). Then, when I’m ok with the list of added values I have another submit button that sends the array of view models to controller.
var vmData = new Array();
...
vmData.push($("form").serializeArray());//not the exact code but the idea is that I'm pushing serialized data from form
// the ajax request
$.ajax({
type:"POST" ,
url: "someURL"
dataType: "json" ,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(vmData)
//data: {vm: JSON.stringify(vmData)}//this generates an error
})
vmData
in Chrome console looks like this (example for 2 array values):
(2)[Array(4), Array(4)]
[
0 : Array(4)
0:{name: "VM_id1" ,value: "123"}
1:{name: "VM_id2" ,value: "99"}
2:{name: "VM_str1" ,value: "string1"}
3:{name: "VM_str2" ,value: "string2"}
1 : Array(4)
0:{name: "VM_id1" ,value: "1"}
1:{name: "VM_id2" ,value: "55"}
2:{name: "VM_str1" ,value: "someval1"}
3:{name: "VM_str2" ,value: "someval1"}
]
vmData
stringified:
[
0: [{name: "VM_id1" ,value: "123"}, 1:{name: "VM_id2" ,value: "99"},{name: "VM_str1" ,value: "string1"} ,{name: "VM_str2" ,value: "string2"}]
1:[{...},{...},{...},{...}]
]
Problem: when data is posted in controller I get 2 list items with null values for all fields. What am I doing wrong?
Another thing, if I manage to solve this, is that I want to pass __RequestVerificationToken
also, but that’s another problem.
Thank you for your time!
2
Answers
So, main problem was that server expected other format of data -> so a flattening of the
serializeArray()
output was required.So this:
Gets flattened to this(which is what server expected):
Also I needed to send
__RequestVerificationToken
to controller as well -> solved with this approach.Finally my
ajax
looks like this:And my
controller action
I do what you are doing, but a slightly different way. So I create a class wrapper like so:
In the form I’ll have:
And I’ll do the following to send it back to the server:
And that can be received by this action:
Your way could work here too; I find having the top-level wrapper has some benefits (it makes it easier to pass along additional data). To include a serialization token, a serialization token is essentially a form element that gets rendered, and as such, it should be included in the form serialization and sent back to the server.