简体   繁体   中英

Accessing ViewBag list via Javascript

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM