Note that there are some explanatory texts on larger screens.

plurals
  1. POIn my asp.net-mvc page, why isn't the right dropdown being selected?
    primarykey
    data
    text
    <p>I have an asp.net-mvc website and i am running into an issue where the correct selected item in a dropdown is not being selected. I have the following code on my controller action (simplified to isolate the issue):</p> <pre><code>public ActionResult MyAction() { var vm = GetVM(); var list = new List&lt;INamed&gt; {new NamedInfo() {Id = 1, Name = "Yes"}, new NamedInfo() {Id = 0, Name = "No"}}; vm.YesNoList = SelectListHelper.GenerateDropdownList(vm.IncludesWeekends ? 1 : 0, list); return View(vm); } </code></pre> <p>and here is the definition of GenerateDropdownList </p> <pre><code> public static List&lt;SelectListItem&gt; GenerateDropdownList&lt;T&gt;(int id, IEnumerable&lt;T&gt; list) where T : INamed { List&lt;SelectListItem&gt; dropdown = list.Select(c =&gt; new SelectListItem { Selected = c.Id == id, Text = c.ToString(), Value = c.Id.ToString() }).ToList(); return dropdown; } </code></pre> <p>Below is the code in my HTML view:</p> <pre><code> &lt;% = Html.DropDownList("IncludesWeekends", Model.YesNoList, new { @id = "IncludesWeekends" })%&gt; </code></pre> <p>I expect No to be selected in my example (and it has Selected = true when i put a breakpoint on the server side but when i look at the html that is generated, nothing is selected:</p> <pre><code>&lt;select id="IncludesWeekends" class="autoComplete1" name="IncludesWeekends"&gt; &lt;option value="1"&gt;Yes&lt;/option&gt; &lt;option value="0"&gt;No&lt;/option&gt; &lt;/select&gt; </code></pre> <p>and "Yes" is selected by default because its teh first item.</p> <p>Any suggestions on what i am doing wrong here or alternatives that work?</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