0

I have the following sample model:

public class FooModel
{
    public List<Bar> myList { get; set; }
}

public class Bar
{
    public string description { get; set; }
    public int pos { get; set; }
    public int myField { get; set; }
}

And, in my cshtml file I have this:

@foreach (var bar in Model.myList)
{
<div class="row">
        <div class="col-2">PLACEHOLDER</div>
</div>
...
}

So basically, the field myField will be a match (when it has some value set) to a pos of another item. What I want, is to autogenerate a dropdown in PLACEHOLDER containing all the descriptions (and pos as value), of all Bar, with the one that matches selected.

I tried this approach (sample) but this shouldn't work, since I am in a foreach inside my Model

@Html.DropDownListFor(col=> col.myField, new SelectList(Model.myList.description, "Id", "Text"), "Choose... ")

Can I do this automatically or will I have to create the dropdown manually?

Thanks!

Diogo Santos
  • 780
  • 4
  • 17
  • related https://stackoverflow.com/questions/20567364/asp-net-mvc-populate-dropdownlist, https://stackoverflow.com/questions/18382311/populating-a-razor-dropdownlist-from-a-listobject-in-mvc – Drag and Drop Sep 28 '20 at 09:48
  • I tried that but the problem is that the "selected" value, here it is inside a foreach loop, and not directly in the model. – Diogo Santos Sep 28 '20 at 09:53

0 Answers0