Mike Flynn
Mike Flynn

Reputation: 24325

knockout.js not working when select list doesn't have options

I think I found a bug in knockout.js in conjuction with the asp.net mvc dropdownlist. When supplying just a list of strings MVC doesn't render the option values on the select element. Knockout.js won't update the value because of this. If I use the second html snippet below by explicitly telling it properties it works. Shouldn't knockout.js read the inner html if the options value isn't available?

Doesn't Update

@Html.DropDownListFor(m => m.Grade, new SelectList((IEnumerable<string>)ViewData[Config.ViewData.Grades], Model.Grade), new { data_bind = "value: selectedGrade" })

Updates

@Html.DropDownListFor(m => m.Grade, new SelectList(((IEnumerable<string>)ViewData[Config.ViewData.Grades]).Select(q => new { Text = q, Value = q }), "Value", "Text", Model.Grade), new { data_bind = "value: selectedGrade" })

The first dropdown renders

<select>
<option>K</option>
<option>2</option>
<option>3</option>
</option>

The second dropdown renders

<select>
<option value="K">K</option>
<option value="2">2</option>
<option value="3">3</option>
</option>

Upvotes: 1

Views: 657

Answers (1)

Michael Best
Michael Best

Reputation: 16688

I'd call it a bug. Please open an issue on Github: https://github.com/SteveSanderson/knockout/issues

Upvotes: 2

Related Questions