i have a code that store data in a ViewBag list which will display on my select dropdown, the problem is my variable x is not recognizing inside the for loop, but when i changed it into index 0 it works. but when i changed into 'x' again it displays error and i can't figure it out why.
error: the name 'x' does not exist in the current context
<script>
var optionSy = '';
{
optionSy += '<select id="SyCode" class="term form-control" required> ';
optionSy += '<option value="" selected hidden>Select School Year</option>';
for (var x = 0; x < "@ViewBag.SysCode.Count"; x++) {
optionSy += '<option value="' + "@ViewBag.SysCode[x].SyCode" + '"> @ViewBag.SysCode[x].SyDesc </option>';
console.log(optionSy)
}
optionSy += '</select>';
}
</script>
it's normal your variable will not be recognized since it's written as string
instead of using @ViewBag.SysCode[x].SyCode"
use the value of your variable @ViewBag.SysCode["+x+"].SyCode"
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.