我正在做 MVC 应用程序,我有一个 DropDownListFor,我需要根据条件禁用一个项目..
这是我的下拉列表
@Html.DropDownListFor(model => Model.SubsidyPlanType, new SelectList(new Dictionary<string, string> { { "", "" }, { "1", "Dollar" }, { "2", "Percentage" }, { "3", "Active" } }, "Key", "Value"), new { id = "Type", @class = "form-control form-select" })
我需要禁用选项 3“活动”
if condition Model.Value = "1"
如何禁用单个项目?
谢谢
尝试以下示例来设置
Disabled
属性,指示是否禁用此 SelectListItem
:
@Html.DropDownListFor(model => Model.SubsidyPlanType,
new Dictionary<string, string> { { "", "" }, { "1", "Dollar" }, { "2", "Percentage" }, { "3", "Active" } }
.Select(d =>
{
return new SelectListItem() { Disabled = (d.Key == "3"), Text = d.Value, Value = d.Key };
}),
new { id = "Type", @class = "form-control form-select" })