OK, here is the "hard way." :) Maybe someone will have a better idea.
First, you will need to distinguish between valid and disabled entries in the selection list, and the only way to do this is either value
or text
option
. Say invalid values will have the value value == "disabled"
, otherwise this is a valid identifier:
//Somewhere in the controller ... var list = yourViewModel.SelectedTeam.TeamPlayers.OrderBy(p => p.LastName).Select(new SelectListItem { Text = item.LastName, Value = item.Disabled ? "disabled" : item.PlayerId.ToString() }).ToList(); //pass it however you want, in the model or by ViewBag ViewBag.MyDropDownList = new SelectList(list, "Value", "Text");
And then in the view
@Html.DropDownListFor(model => model.Position1, ViewBag.MyDropDownList as SelectList);
$(document).ready(function() { $(#"Position1 option[value=\"disabled\"]").prop("disabled",true); });
source share