C# ASP.NETMVC中的级联DropDownList和ListBox

C# ASP.NETMVC中的级联DropDownList和ListBox,c#,asp.net-mvc,asp.net-mvc-4,listbox,C#,Asp.net Mvc,Asp.net Mvc 4,Listbox,我正在Asp.NETMVC中尝试级联dropdowlist和listbox的示例。我已经看到了级联dropdownlists的例子,但是它使用JSON来填充第二个dropdownlist。所以,请不要将这个问题标记为重复 我有一个下拉列表,用于显示地区/县,以及一个列表框,该列表框在视图页面中显示使用模型预先填充的客户。当我在dropdownlist中选择一个县/地区时,我需要在列表框中获得一个经过过滤的客户列表,通过使用查询,我更喜欢linq 我的问题是,我将在控制器中的何处写入查询 型号:

我正在Asp.NETMVC中尝试级联dropdowlist和listbox的示例。我已经看到了级联dropdownlists的例子,但是它使用JSON来填充第二个dropdownlist。所以,请不要将这个问题标记为重复

我有一个下拉列表,用于显示地区/县,以及一个列表框,该列表框在视图页面中显示使用模型预先填充的客户。当我在dropdownlist中选择一个县/地区时,我需要在列表框中获得一个经过过滤的客户列表,通过使用查询,我更喜欢linq

我的问题是,我将在控制器中的何处写入查询

型号:

public class ReportParameterCustomerCard
{
  [Required(ErrorMessage = "Please select a district")]
  [Display(Name = "District")]
  public int District { get; set; }
  public System.Web.Mvc.SelectList languanges { get; set; }
  public string[] selectedCustomer { get; set; }
  public IEnumerable<SelectListItem> allCustomer { get; set; }
  public string CustomerCode { get; set; }
}
公共类报告参数CustomerCard
{
[必需(ErrorMessage=“请选择一个地区”)]
[显示(Name=“District”)]
公共int区{get;set;}
public System.Web.Mvc.SelectList语言{get;set;}
公共字符串[]selectedCustomer{get;set;}
公共IEnumerable allCustomer{get;set;}
公共字符串自定义代码{get;set;}
}
控制器:

public ActionResult CustomerbyDistrictReport()
{
  ViewBag.ShowIFrame = false;
  ReportParameterCustomerCard cus = new ReportParameterCustomerCard();
  List<SelectListItem> list = new List<SelectListItem>();
  Helpers.DataLayer dl = new Helpers.DataLayer();
  DataSet ds = dl.ReturnDataset("[Sales].[County]", "*");
  for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  {
    list.Add(new SelectListItem { Text = ds.Tables[0].Rows[i]["County_Name"].ToString(), Value = ds.Tables[0].Rows[i]["RowNum"].ToString() });
  }
  cus.languanges = new SelectList(list, "Value", "Text");
  cus.selectedCustomer = null;
  cus.allCustomer = GetallCustomer();
  return View(cus);
}
公共操作结果CustomerbyDistrictReport()
{
ViewBag.ShowIFrame=false;
ReportParameterCustomerCard cus=新的ReportParameterCustomerCard();
列表=新列表();
Helpers.DataLayer dl=新Helpers.DataLayer();
数据集ds=dl.ReturnDataset(“[Sales].[County]”,“*”);
对于(int i=0;i
视图:


a、 District,Model.languages,“--选择一个--”,新的{id=“ddldDistrict”,style=“宽度:255px;高度:25px;”})%>
a、 selectedCustomer,Model.allCustomer,新建{@class=“selected”,multiple=“multiple”,style=“width:250px;”,tabindex=“4”})%>

例如,我在这里放了一些代码。请一定要喜欢

$('#firstDropDown').on('change', function() {
    var getValue = $(this).val();
    $.ajax({
        url: '@url.Action("secondDrodownFetchValue"), new { myValue : getValue }',
        type: 'GET',
        success: function (result) {
             $.each(result, function (index, value) {
                    $('#secondDorpDown').append($('<option>').text(value).attr('value', value));
                });
        }
    });
})
$('firstDropDown')。在('change',function()上{
var getValue=$(this.val();
$.ajax({
url:'@url.Action(“SecondDrownFetchValue”),新的{myValue:getValue}',
键入:“GET”,
成功:功能(结果){
$.each(结果、函数(索引、值){
$('#secondorpdown').append($('').text(value.attr('value',value));
});
}
});
})
我的控制器动作方法

[HttpGet]
public ActionResult secondDrodownFetchValue(string myValue)
{
    // your code
    return List<string>(); // Here i pass the empty value You have to pass the list of customer .
}
[HttpGet]
public ActionResult SecondDrownFetchValue(字符串myValue)
{
//你的代码
return List();//这里我传递空值,您必须传递客户列表。
}

您是否不想使用javascript/jquery(在这种情况下,您需要在每次选择地区时提交表单)?您的视图没有包含该地区的下拉列表?请查看问题中的编辑。不幸的是,我复制粘贴了一个不同的视图。所有教程都描述了使用Json使用javascript处理dropdownlist。为什么不想使用javascript/jquery填充列表框?请尝试上面的代码,我现在已经更新了它。请这样试试,让我知道。
[HttpGet]
public ActionResult secondDrodownFetchValue(string myValue)
{
    // your code
    return List<string>(); // Here i pass the empty value You have to pass the list of customer .
}