Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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# 在更改第一个ddl时使用jQueryAjax绑定下拉列表_C#_Jquery_Asp.net_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 在更改第一个ddl时使用jQueryAjax绑定下拉列表

C# 在更改第一个ddl时使用jQueryAjax绑定下拉列表,c#,jquery,asp.net,asp.net-mvc-4,razor,C#,Jquery,Asp.net,Asp.net Mvc 4,Razor,我有两个下拉列表,在更改第一个下拉列表后,我想在ajax中填充第二个下拉列表。 我在ajax中获得SelectListItem如何将其传递到下拉列表以绑定它 public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id) { Pol = new SelectList(GetData(), "SecondID", "Name"); ViewBag.Pol = Pol

我有两个下拉列表,在更改第一个下拉列表后,我想在ajax中填充第二个下拉列表。 我在ajax中获得SelectListItem如何将其传递到下拉列表以绑定它

  public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id)
    {

        Pol = new SelectList(GetData(), "SecondID", "Name");


        ViewBag.Pol = Pol;

        return Pol;
    }
视图:

  public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id)
    {

        Pol = new SelectList(GetData(), "SecondID", "Name");


        ViewBag.Pol = Pol;

        return Pol;
    }
控制器:

  public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id)
    {

        Pol = new SelectList(GetData(), "SecondID", "Name");


        ViewBag.Pol = Pol;

        return Pol;
    }
public IEnumerable buildSecondDropDownList(int-id)
{
Pol=新的选择列表(GetData(),“SecondID”,“Name”);
ViewBag.Pol=Pol;
返回Pol;
}

首先修复控制器操作,使其返回JSON,而不是一些
IEnumerable
。请记住,在ASP.NET MVC中,控制器操作必须返回操作结果:

  public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id)
    {

        Pol = new SelectList(GetData(), "SecondID", "Name");


        ViewBag.Pol = Pol;

        return Pol;
    }
public ActionResult BuildSecondDropDownLists(int id)
{
    var result = GetData();
    return Json(result, JsonRequestBehavior.AllowGet);
}
然后循环遍历返回的元素并将它们附加到第二个下拉列表中:

  public IEnumerable<SelectListItem> BuildSecondDropDownLists(int id)
    {

        Pol = new SelectList(GetData(), "SecondID", "Name");


        ViewBag.Pol = Pol;

        return Pol;
    }
success: function (result) {
    var secondDdl = $('#SecondID');
    secondDdl.empty();
    $.each(result, function() {
        secondDdl.append(
            $('<option/>', {
                value: this.SecondID,
                html: this.Name
            })
        );
    });
}
成功:函数(结果){
var secondDdl=$(“#SecondID”);
secondDdl.empty();
$.each(结果,函数(){
secondDdl.append(
$('', {
值:this.SecondID,
html:这个名字
})
);
});
}

你好,达林,谢谢你的上述建议。我遇到了一个lil问题,我在一个新的帖子中发布了关于您的解决方案;任何帮助都将不胜感激!