Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 第二个下拉列表未进行筛选_C#_Asp.net Mvc - Fatal编程技术网

C# 第二个下拉列表未进行筛选

C# 第二个下拉列表未进行筛选,c#,asp.net-mvc,C#,Asp.net Mvc,我正在制作一个应用程序,它允许用户从服务列表中选择一项服务,并从员工列表中选择一名员工。当用户选择一项服务时,员工的下拉列表必须自动过滤,以显示能够执行该服务或具有该技能的员工。例如,如果客户选择理发,则只有技能为理发的员工才能筛选到下拉列表中。我将向您展示我的代码,注意:我不想在服务和员工之间创建关系 第一个dropdownlist正在填充,而不是第二个dropdownlist。如果我将值硬编码到服务所需的dropdownlist中,则过滤器工作。如果我使用服务表中的值填充Service Re

我正在制作一个应用程序,它允许用户从服务列表中选择一项服务,并从员工列表中选择一名员工。当用户选择一项服务时,员工的下拉列表必须自动过滤,以显示能够执行该服务或具有该技能的员工。例如,如果客户选择理发,则只有技能为理发的员工才能筛选到下拉列表中。我将向您展示我的代码,注意:我不想在服务和员工之间创建关系

第一个dropdownlist正在填充,而不是第二个dropdownlist。如果我将值硬编码到服务所需的dropdownlist中,则过滤器工作。如果我使用服务表中的值填充Service Required dropdownlist,则过滤器不起作用。请查看下面的代码

 public class EmployeeSkillVM
{
    public string ServiceRequired { get; set; }
    public string EmployeeChoice { get; set; }
}

 public ActionResult Index()
    {
        List<Service> ServiceList = db.Services.ToList();
        ViewBag.ServiceList = new SelectList(ServiceList, "ServiceID", "ServiceName");
        return View();
    }

    public JsonResult GetEmpList(string ServiceRequired)
    {
        db.Configuration.ProxyCreationEnabled = false;
        List<Employee> EmpList = db.Employees.Where(x => x.Skills == ServiceRequired).ToList();
        return Json(EmpList, JsonRequestBehavior.AllowGet);

    }

 @model SkillFiltering.Models.EmployeeSkillVM
 @{
  ViewBag.Title = "Index";
  }

 <h2>Index</h2>
//这很有效

//这行不通


您使用ServiceRequired来获取第二个下拉列表的所需输入,但没有将ServiceRequired用作第一个下拉列表的ID,或者您可以将其用作css类ServiceRequired,在视图中,用以下内容替换下拉列表:

@Html.DropDownListFor(model => model.ServiceRequired, new SelectList(ViewBag.ServiceList, "ServiceID", "ServiceName"), "- Please Select -", new { @class = "form-control" })
@Html.DropDownListFor(model => model.EmployeeChoice, new SelectList(" "), "--Select State--", new { @class = "form-control" })
您的索引操作方法将是:

 public ActionResult Index()
    {
        List<Service> ServiceList = db.Services.ToList();
        ViewBag.ServiceList = ServiceList;
        return View();
    }

在循环中添加一个警报“test”以查看它是否到达那里。您能告诉我在$.eachdata、函数索引、行之前在哪里添加该警报吗{你在ServiceRequired中得到了什么值?你在Employee数据库表中有三个Sam值?什么是EmployeeSkillVM?你的索引操作方法没有发送任何EmployeeSkillVM来查看。亲爱的@GhulamMasudh,我复制了你的代码,并成功地构建和运行了上述代码。
 <script src="~/Scripts/jquery-1.10.2.min.js"></script>
 <script>
     $(document).ready(function () {
    $("#ServiceRequired").change(function () {
        $.get("/Home2/GetEmpList", { ServiceRequired: $("#ServiceRequired").val() }, function (data) {
            $("#EmployeeChoice").empty();
            $.each(data, function (index, row) {
                $("#EmployeeChoice").append("<option value='" + row.EmployeeID + "'>" + row.EmployeeName + "</option>")
            });
        });
    })
});
 </script>
@Html.DropDownListFor(model => model.ServiceRequired, new SelectList(ViewBag.ServiceList, "ServiceID", "ServiceName"), "- Please Select -", new { @class = "form-control" })
@Html.DropDownListFor(model => model.EmployeeChoice, new SelectList(" "), "--Select State--", new { @class = "form-control" })
 public ActionResult Index()
    {
        List<Service> ServiceList = db.Services.ToList();
        ViewBag.ServiceList = ServiceList;
        return View();
    }