根据另一个DropDownList的选定值填充一个DropDownList

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

我试图基于MVC .NET Core 2.2中另一个DropDownList的值来填充DropDownList。

<div class="form-group row">
    <label class="col-sm-2 col-form-label">Type :</label>
        <div class="col-sm-10">
            <select asp-for="SelectedType" asp-items="@(new SelectList(Model.TypeList, "TypeName", "TypeDescription"))" class="custom-select custom-select-sm">
            </select>
        </div>
</div>

public class Type
{
    public string TypeName { get; set; }
    public string TypeDescription { get; set; }
}

我不熟悉JavaScript或Ajax,无法在Google上弄清楚示例。

所以基本上,如果您在DropDownList“ Type”中选择例如“ A”,则DropDownList需要填充基于“ A”的字符串列表,当您选择“ B”时,DropDownList必须填充一个列表基于“ B”的字符串。

有人可以给我一个非常简单的例子吗?

javascript c# razor dropdown asp.net-core-2.2
1个回答
0
投票
        <asp:DropDownList ID="DropDownBox2" runat="server" Width="168px">
            <asp:ListItem Value="%" Text="Option 1"></asp:ListItem>
            <asp:ListItem Value="Option 2"></asp:ListItem>
            <asp:ListItem Value="Option 3"></asp:ListItem>
            <asp:ListItem Value="Option 4"></asp:ListItem>
            <asp:ListItem Value="Option 5"></asp:ListItem>                
            <asp:ListItem Value="Option 6"></asp:ListItem>

在pageload中

        if (DropDownList1.Text == "Condition 1")
    {
        DropDownBox2.Items[3].Enabled = false;
        DropDownBox2.Items[4].Enabled = false;
        DropDownBox2.Items[5].Enabled = false;
    }

这将使选项0,1,2仅在第二个下拉框中可见。

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