Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement multiple dropdown lists in MVC
    primarykey
    data
    text
    <p>Let's say I want to show multiple Drop Down Boxes each with the same values within them within a View using the @Html.DropDownList. Is there an easy way to be able to render all of these Drop Down Boxes without having to bind something like the ViewBag to each of the months returned?</p> <p>For example:</p> <p>Controller</p> <pre><code>List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;(); var values = (from v in db.Values select v); foreach (var value in values) { items.Add(new SelectListItem { Text = value.Name, Value = value.Id }); } ViewBag.Values = PopulateSelectList("Values"); </code></pre> <p>View</p> <pre><code>@Html.DropDownList("Values") </code></pre> <p>But what the goal would be is to have something like</p> <pre><code>&lt;div class="control-group"&gt; @Html.LabelFor(model =&gt; model.DesignStyle) &lt;div class="controls"&gt; @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") @Html.DropDownList("Values") &lt;/div&gt; &lt;/div&gt; </code></pre> <p>My guess is I would need some sort of <code>@foreach (item in Model)</code> to populate each of the DropDowns on the View</p> <p>Here is a sample of my initial thought on how to get something going.</p> <pre><code>List&lt;SelectListItem&gt; results = new List&lt;SelectListItem&gt;(); SelectListItem item = null; var values = (from v in db.Values select v); for (int i = 0; i &lt;= 15; i++) { item = new SelectListItem(); item.Text = "Select a value"; item.Value = "1"; results.Add(item); foreach (var thread in threads) { item.Text = thread.Name; item.Value = thread.Id; results.Add(item); } } </code></pre> <p>What I don't want to do is duplicate the logic to populate a SelectListItem multiple times and put each of those in a separate ViewBag item.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload