Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
How to use the JavaScript loop index in the ViewBag list string

I want to use the JavaScript index to get the value of the viewBag list. But I have a mistake in combining the two. Thanks for guiding me

<script>
    for (var i = 0; i < @(Enumerable.Count(ViewBag.paramProperty)); i++) {
        select: `@(ViewBag.paramProperty[ + "${i}" + ]);
        var element = document.querySelectorAll(`[value="${select}"]`);
        element[0].setAttribute("checked", "checked");
    }
</script>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Firstly,you need to make sure you JsonConvert.SerializeObject with ViewBag.paramProperty,for example:

ViewBag.paramProperty = JsonConvert.SerializeObject(new List<string> { "a", "b", "c" });

Then try to set a js variable with ViewBag.paramProperty:

var paramProperty = @Html.Raw(JsonConvert.DeserializeObject(ViewBag.paramProperty));

and you can use:

for (var i = 0; i < paramProperty.lenght; i++) {
        select: paramProperty[i];
        var element = document.querySelectorAll(`[value="${select}"]`);
        element[0].setAttribute("checked", "checked");
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand your code, you are trying to use a server-side variable in client-side. You can try to declare a javascript variable "paramProperty" and set its value to the ViewBag.paramProperty and then iterate over it

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda