Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Matthieu , I tested your approach and also my own way where I use the Controller instead to fill the Dictionary and the SelectList. Then I make the View inherit from the model and show the DropDownList. But both won't work. Wouldn't it then be easier to pass the SelectList through the ViewData and then show it in the View? Example:</p> <p>Controller</p> <pre><code>var rowDict = new Dictionary&lt;int, int&gt;(); for (var i = 1; i &lt; 11; i++) rowDict.Add(i, i); var rows = new SelectList(rowDict, "Key", "Value", 6); ViewData["Rows"] = rows; return View(); </code></pre> <p>View</p> <pre><code>&lt;%: Html.DropDownList("ItemsDropDown", (SelectList)ViewData["Rows"]) %&gt; </code></pre> <p><strong>Edit:</strong> Thanks to Ukko for the tip of the selected parameter to be a string, the code I tried worked as well, and you don't need to fill the SelectList through the constructor of the ViewModel. The idea alone is actually illogical as it is in the controller you need to fill the ViewModel and pass this to the View:</p> <p>Model:</p> <pre><code>public class HomeModel { public SelectList Rows { get; set; } public String Selected { get; set; } } </code></pre> <p>Controller</p> <pre><code>var rowDict = new Dictionary&lt;int, int&gt;(); for (var i = 1; i &lt; 11; i++) rowDict.Add(i, i); var rows = new SelectList(rowDict, "Key", "Value"); var model = new HomeModel { Rows = rows, Selected = "6" }; return View(model); </code></pre> <p>View:</p> <pre><code>&lt;div class="editor-field"&gt; &lt;%: Html.DropDownListFor(model =&gt; model.Rows, Model.Rows, Model.Selected) %&gt; &lt;/div&gt; </code></pre>
    singulars
    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.
    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