根据从另一个DropDownList中选择的值填充一个DropDownList

我试图根据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中选择“类型”,例如“ A”,则需要使用基于“ A”的字符串列表来填充DropDownList,而当您选择“ B”时,则需要使用a来填充DropDownList。基于“ B”的字符串列表。

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

像这样吗?

$(document).ready(function()  
{  
    $("#ddlEmployee").on("change",function()  
    {  
        $.ajax(  
        {  
            url: '/Home/GetTypesBasedOnValueFromOtherDropDownList' + $(this).attr("value"),type: 'GET',data: "",contentType: 'application/json; charset=utf-8',success: function(data)  
                {  
                    $("#???").html(data);  
                },error: function()  
            {  
                alert("error");  
            }  
        });  
    });  
}); 
< /script> 
lttsusan 回答:根据从另一个DropDownList中选择的值填充一个DropDownList

        <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>

在page_load

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

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

如果您需要即时且没有page_load的信息,请参见以下jscript解决方案的工作示例:https://jsfiddle.net/k148pk76/1/

,

您可以将第一个select的选定值传递给服务器端,查询数据库并将结果返回给客户端,这是ajax的成功回调函数填充第二个select的结果。以下代码供您参考:

$("#SelectedType").on("change",function () {                
    $.ajax({
        type: 'GET',url: "/home/GetTypesBasedOnValueFromOtherDropDownList",contentType: 'application/json',data: { Type: $("#SelectedType").val() },success: function (data) {
            var s = '<option value="-1">Please Select a Course</option>';
            for (var i = 0; i < data.length; i++) {
                s += '<option value="' + data[i].courseCode + '">' + data[i].courseName + '</option>';
            }
            $("#secondSelect").html(s);  
        }
    });
})

服务器端:

public IActionResult GetTypesBasedOnValueFromOtherDropDownList(string Type)
{
    //you can query database instead 
    List<AdmInstCourses> admInstCourses = new List<AdmInstCourses>();
    admInstCourses.Add(new AdmInstCourses() { CourseCode = 1,CourseName = "name1",Units = "3.2" });
    admInstCourses.Add(new AdmInstCourses() { CourseCode = 2,CourseName = "name2",Units = "3.3" });

    return new JsonResult(admInstCourses);
}
本文链接:https://www.f2er.com/3150118.html

大家都在问