Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!