0

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?

Edit:

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

Javascript:

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 });

$.ajax({
        url: "/Home/CollectJson1/",
        type: 'POST',
        contentType: "application/json",
        dataType: 'html',
        data: StringToPost,
        success: function (data) {
        console.log(data);      
        }
      });

Controller:

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

        Return Json(Step3Object)

    End Function

Classes:

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:

{"Step3Object":{"BlockEntrances":{"BlockEntranceButtons":[[1,2,3,4,5,6,7,8],[8,7,6,5,4,3,2,1],[5,5,5,5,5,5,5,5]]}}}
2
  • no, because array indexes don't have names. And if they did, you'd have 3 called BlockEntranceButtons. How would you distinguish them? Did you mean to add an object at each index of BlockEntrances, which has within it an ID and then another property called BlockEntranceButtons? That would make some sort of logical sense - a set of buttons for 3 different entrances? Commented Oct 5, 2016 at 14:22
  • Yes that's what i meant, sorry if point did not come across clearly Commented Oct 5, 2016 at 14:25

1 Answer 1

1

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):

JS:

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] }
];

VB:

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

<HttpPost>
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.