Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda