I want to display the strings from my variable allCompanies in a dropdown. I tried to list it with javascript like this, but it doesn't work:
function Requests(){
var companiesList = @Html.Raw(Json.Serialize(allCompanies));
for (var i = 0; i <= companiesList.length - 1; i++) {
$('#Company').append('<option value="' + companiesList[i] + '">' + companiesList[i] + '</option>');
}
}
My html code:
<div class="row">
<div class="col-md-6">
<p>Select company to see requests: </p>
<select id="Company" name="Company" class="form-control custom-select">
<option value="">Select company.</option>
</select>
</div>
</div>
As far as I can see in your HTML code, you are not actually calling the function you created at all under specific call onKeyUp, onChange etc.
Since you only want to fill the select options in order to initialize them (if I am getting this right), you could just initialize them after the HTML page has loaded.
So I would suggest you to simply use your code in the $(document).ready(function()){} as shown below :
$( document ).ready(function() {
var companiesList = @Html.Raw(Json.Serialize(allCompanies));
for (var i = 0; i <= companiesList.length - 1; i++) {
$('#Company').append('<option value="' + companiesList[i] + '">' + companiesList[i] + '</option>');
}
});