I have a drop down list in asp.net using nested for loops in a view, as well as variables I’ve
passed from a controller. The value that is displayed to the user is supposed to be the name of a stop, and the value is an id of that stop. Both the names and the stop ids are in lists I’ve passed from the controller to the view. But, when the drop down list is displayed, the first stop name is repeated for the entire list. I know that the right stop id is being selected, but the name is repeating. Here is my code:
<select id="stop" onchange="sendInfo();">
@foreach(var blah in ViewBag.foobar)
{
foreach (var foo in ViewBag.stops)
{
<option value="@foo">@blah</option>
}
}
</select>
foo is the stop id that is correctly used, but blah is the name of the stop name, which is repeated. I feel like I’m so close but not quite there. I hope this was clear enough, it is confusing to explain.
I’m not positive I understand what you’re saying, but if there’s a 1:1 relationship between stops and IDs, why don’t you make a class containing both, like
and pass in a list of those and do a single loop: