@html.dropdownlist用于在条件满足时禁用一项

问题描述 投票:0回答:1

我正在做 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"

如何禁用单个项目?

谢谢

c# asp.net-mvc html.dropdownlistfor
1个回答
0
投票

尝试以下示例来设置

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" })

© www.soinside.com 2019 - 2024. All rights reserved.