我正在尝试根据条件禁用下拉列表,我正在使用:
var disabledAttr = exists ? "disabled=true" : ""; //exists is a boolean of course
@Html.DropDownListFor(x => x.Input.Divisions[index].DivisionPlayers[participantIndex].Team[playerIndex].Id,
new SelectList(Model.ParticipantsList, "Value", "Text", selected),
new { @class = "participant form-control", disabledAttr })
,但是选择框将属性呈现为:
disabledattr="disabled=true"
我如何才能将其呈现为简单的“ disabled = true”?
答案 0 :(得分:1)
您的问题是您要向名为 disabledAttr 的匿名对象追加一个属性,该属性的值为 disabled = true 。
您需要实现的目标是拥有名为 disabled 的属性,其值为 true (或“ true” < / em>,不确定哪一个有效,我现在无法对其进行测试。)
快速而肮脏的解决方案:
@{
string @class = "participant form-control";
object attrs = exists ? (object)new {@class, disabled = true} : new {@class};
}
@Html.DropDownListFor(x => x.Input.Divisions[index].DivisionPlayers[participantIndex].Team[playerIndex].Id,
new SelectList(Model.ParticipantsList, "Value", "Text", selected), attrs )