In my MVC3 application i have a dropdown list. In there i have to show all the results, but some results must be disabled, so that they are not selectable. How can i do that?
This is what i have right now. It simply doesn’t show the players that have Disabled set to true.
@Html.DropDownListFor(m => m.Position1, Model.SelectedTeam.TeamPlayers
.Where(c => c.Player.Disabled == false)
.OrderBy(t => t.Player.Lastname)
.ToSelectList(m => m.FullName, m => m.PlayerId))
So is there another way to also show the Disabled players, but that the output would be like this, instead of just hiding them completely:
<select>
<option>Player 1</option>
<option disabled="disabled">Player 2</option>
<option>Player 3</option>
<option>Player 4</option>
<option disabled="disabled">Player 5</option>
<option>Player 6</option>
</select>
Is that possible with a DropDownListFor?
OK, here’s the ‘hard way’ of doing it. 🙂 Maybe someone else will have a better idea.
First, you’d need to differ between valid and disabled entries on the select list – and only way to do it is either by
valueortextof theoption. Let’s say the invalid values will have thevalue == "disabled", otherwise it’s a valid ID:And then in the view