Im trying to parse a simple array of guids/strings to json
the json is used as a parameter on a post call to my controller.
I have been trying a lot of different things but i simply cant make the controller method see my List.
The array is built from checkbox values:
$('input:checkbox').click(function () {
if ($(this).attr("checked") == true) {
selected.push($(this).val());
}
});
and looks alright.
then i try to convert it to JSON
$(document).ready(function () {
$('#Summarize').click(function () {
var arrayJson = {};
for (i in selected) {
arrayJson[i] = selected[i];
}
var json = {
SelectedQuantities: arrayJson
};
$.ajax({
url: "/MVC/Physical/SelectQuantities/@Model.TopID",
type: "POST",
data: JSON.stringify(json),
dataType: "json",
contentType: 'application/json',
success: function (result) {
debugger;
if (result.status == 200)
location.replace = "/MVC/Physical/QuantitySummaryView/@Model.TopID"
else {
//Handle error
}
}
});
});
});
My controller method looks as following:
public ActionResult QuantitySummaryView(Guid id, List<String> SelectedQuantities)
But the list is always null. I figured it was because the JSON list doesnt contain the array of GUIDs directly underneath it.
Can you help me get it nudged in place?
JSON (as seen by Chromes dev tools) looks as following:
{
"SelectedQuantities": {
"0": "707c40bd-4434-41ed-80fd-4ac541a81e85",
"1": "a8d78a4b-b107-4e1c-97b5-5d8abf530ba8",
"2": "a19226cc-9b22-4174-97e3-bb003d4b2746"
}
}
I think you are serializing one too many times, and you have one too many layers. Try this:
I removed one of the SelectedQuantities, and change the ajax call.
Also, try Firebug, it will show you want was passed to the server, so you can make sure the json created and passed is correct.