A Houghton A Houghton - 11 months ago 46
Vb.net Question

Passing an array of objects to mvc controller using json

I've attempting to model bind Json data to a property class in MVC. Object 2 is passed in to the controller OK. Object 3 is a list of lists and I'm not sure my data structure is correct in javascript.

I'm receiving the BlockEntrances data (size of array) but not the BlockEntranceButtons data.

"BlockEntranceButtons" does not appear in the Json string when i believe it should be there.

Could someone please point me in the right direction?


Altered step3 in to 2 objects. Json string looks more like i want but my controller is still receiving a null value.


Step2 = {};
Step2.MainEntranceButtons = [0, 1, 2, 3, 4, 5, 6, 7, 8]

Step3 = {};
Step3.BlockEntrances = {};
Step3.BlockEntrances.BlockEntranceButtons = [];
Step3.BlockEntrances.BlockEntranceButtons[0] = [1, 2, 3, 4, 5, 6, 7, 8];
Step3.BlockEntrances.BlockEntranceButtons[1] = [8, 7, 6, 5, 4, 3, 2, 1];
Step3.BlockEntrances.BlockEntranceButtons[2] = [5, 5, 5, 5, 5, 5, 5, 5];

var StringToPost = JSON.stringify({ Step2Object: Step2, Step3Object: Step3 });

url: "/Home/CollectJson1/",
type: 'POST',
contentType: "application/json",
dataType: 'html',
data: StringToPost,
success: function (data) {


Function CollectJson1(ByVal Step2Object As Step2Data, ByVal Step3Object As Step3Data) As JsonResult

Return Json(Step3Object)

End Function


Public Class Step2Data

Public Property MainEntranceButtons As List(Of String)

End Class

Public Class Step3Data

Public Property BlockEntrances As List(Of Step3SubData)

End Class

Public Class Step3SubData

Public Property BlockEntranceButtons As List(Of String)

End Class

Json Data posted for Object3:


Answer Source

Array indexes don't have names, so it doesn't work the way you were expecting in the original version of the question. And if they did, you'd have 3 called BlockEntranceButtons. How would you distinguish them?

What you've now got is similar to what I think would be more suitable:

Step3.BlockEntrances =  [  
  { EntranceID: 1, BlockEntranceButtons: [1,2,3,4,5,6,7,8] }, 
  { EntranceID: 2, BlockEntranceButtons: [8,7,6,5,4,3,2,1] }, 
  { EntranceID:3, BlockEntranceButtons: [5,5,5,5,5,5,5,5] }

What you're trying to do doesn't really match up with the server-side code structure though, and the structure looks a bit confused to my eyes.

If it was me I would treat all entrances as the same, and just have a flag to say if it's the main entrance or a block entrance. Then you don't need a separate data structure for each. Maybe something like (bearing in mind I don't know your exact requirements):


var Entrances =  [  
  { EntranceID: 1, BlockID: "main", EntranceButtons: [0,1,2,3,4,5,6,7,8] }, 
  { EntranceID: 2, BlockID: "A", EntranceButtons: [8,7,6,5,4,3,2,1] }, 
  { EntranceID: 3, BlockID: "A", EntranceButtons: [9,5,1,7,3,4,2,1] }, 
  { EntranceID: 4, BlockID: "B", EntranceButtons: [5,5,5,5,5,5,5,5] }


Public Class Entrance
  public Property EntranceID As Int
  public Property BlockID As String
  public Property EntranceButtons As Int()
End Class

Function CollectJson1(ByVal Entrances As List(Of Entrance)) As JsonResult

    Return Json(Entrances)

End Function

If necessary, you'd probably end up with a class called Block as well to represent the different Blocks, with an ID and a Description.