5

I'm trying to access the elements of a list stored in ViewBag as follows:

function equipamentoTemControle() {

   for(i = 0; i < @ViewBag.qtdEquipamentos; i++) {

      var contratocod = @ViewBag.DadosEquipamentos[i].contratocod;
   }

}

But when trying to access contratocod of attribute index i the Visual Studio says that the variable i does not exist. How do I access ?

1
  • An integer property Commented Sep 9, 2016 at 15:25

1 Answer 1

15

Use

var jsonObj = @Html.Raw(Json.Encode(ViewBag.qtdEquipamentos));

and then

for (i = 0; i < jsonObj .length; i++) { 
var contratocod = jsonObj[i].contratocod;
}

Hope this work first encode model in a JSON and then iterate.

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.