I have these Models.
public class ModelA { List<ModelB> ModelB {get;set;} } public class ModelB { List<ModelC> ModelC {get;set} }
Now, I tried to convert in my script the models above base from this answer in my view.
<script> var modelB = '@Html.Raw(Json.Encode(Model.ModelB))'; var modelBData = JSON.parse(modelB); // Here comes the problem... var modelC = '@Html.Raw(Json.Encode(Model.ModelB.ModelC))'; </script>
I can convert the Model B to javascript array but failed to convert ModelC into a javascript array. Now, how can I convert the list of ModelC in ModelB into a javascript array? I was hoping someone might be able to spot where i'm going wrong.
ModelA
contained threeModelB
items. The firstModelB
contains oneModelC
. The secondModelB
contains twoModelC
. The third contains three. How manyModelC
items are you expecting to see in yourHere comes the problem
JSON?modelB
andmodelBData
lines? (after commenting outvar modelC
and other lines that don't compile)