当值等于“*”时,我需要动态禁用输入。如何使用MVC Razor实现这一目标?
@Html.TextBoxFor(m => m.Digits[0], new { @class = "form-control input-lg label_16", @placeholder =
"1st", (Model.Digits[0] == "*" ? "disabled" : "") })
以上代码无法编译 这可能吗?
答案 0 :(得分:12)
尝试使用三元运算符
@Html.TextBoxFor(m => m.Digits[0], Model.Digits[0] == "*" ? (object)new { @class = "form-control input-lg label_16", @placeholder =
"1st", @disabled = "disabled" } : new { @class = "form-control input-lg label_16", @placeholder =
"1st" })
在上面的代码中,@Html.TextBoxFor
辅助方法的第二个参数将基于Model.Digits[0]
的值。如果是*
,则参数将包含disabled
属性
new { @class = "form-control input-lg label_16", @placeholder =
"1st", @disabled = "disabled" }
否则
new { @class = "form-control input-lg label_16", @placeholder =
"1st" }