【问题标题】:Fill a DropDownList based on the selected value from another DropDownList根据从另一个 DropDownList 中选择的值填充 DropDownList
【发布时间】:2020-03-03 06:05:34
【问题描述】:

我试图根据 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”,DropDownList 需要填充基于“A”的字符串列表,当你选择“B”时,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> 

【问题讨论】:

    标签: javascript c# razor dropdown asp.net-core-2.2


    【解决方案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);
    }
    

    【讨论】:

      【解决方案2】:
              <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>
      

      在页面加载中

              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/

      【讨论】:

      • 它的 asp.net ,在 WPF 或 UWP 应用程序中,我也知道如何做到这一点。网页新手
      • 这支持.NET Core 2.2?
      • 这个例子取自旧网站的代码。它的目标框架是2.0。应该工作。
      • 但是selected item changed事件是怎么触发到控制器的呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多