C# ASP.NET MVC级联组合框

C# ASP.NET MVC级联组合框,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在尝试根据上面的组合框选项刷新组合框。 我有控制器的以下代码: 根据第一个组合框的选择填充第二个组合框的控制器方法代码 public JsonResult Testeeee(int id_cat ) { var result = new dificuldade(); SqlCommand com; string str; SqlConnection conn = new SqlConnection(@"Server=DESKTOP-4GPISBO\SQLE

我正在尝试根据上面的组合框选项刷新组合框。 我有控制器的以下代码:

根据第一个组合框的选择填充第二个组合框的控制器方法代码

public JsonResult Testeeee(int id_cat )
{
    var result = new dificuldade();

    SqlCommand com;

    string str;
    SqlConnection conn = new SqlConnection(@"Server=DESKTOP-4GPISBO\SQLEXPRESS;Database=Jogo;Trusted_Connection=True;");
    conn.Open();

    str = "select tipo_dificuldade, dificuldade.id_dificuldade " +
           "from dificuldade " +
           "INNER JOIN palavra ON palavra.id_dificuldade = dificuldade.id_dificuldade " +
           "where palavra.id_cat = '" + id_cat + "'  ";

    com = new SqlCommand(str, conn);
    SqlDataReader reader = com.ExecuteReader();
    if (reader.Read())
    {
        result.TIPO_DIFICULDADE = reader["tipo_dificuldade"].ToString();
        result.id_dificuldade = int.Parse(reader["id_dificuldade"].ToString());

    }
    conn.Close();

    return Json(result, JsonRequestBehavior.AllowGet);
}
这是我的视图代码:

@Html.DropDownListFor(model => model.id_cat, ViewBag.ListaCategoria as SelectList, "-- Selecione a Categoria --", new { @class = "form-control" })
@Html.DropDownListFor(model => model.id_dificuldade, new SelectList(" "),  "-- Selecione a Dificuldade --",  new { @class = "form-control"  })
jQuery代码:

$(document).ready(function () {
    $("#id_cat").change(function () {
        $.get("/Home/Testeeee", { id_cat: $("#id_cat").val() }, function (data) {
            $("#id_dificuldade").empty();
            $.each(data, function (index, row) {
                $("#id_dificuldade").append("<option value='" + row.id_dificuldade + "'>" + row.TIPO_DIFICULDADE + "</option>")
            });
        });
    })
});
$(文档).ready(函数(){
$(“#id_cat”)。更改(函数(){
$.get(“/Home/testeee”,{id_cat:$(“#id_cat”).val()},函数(数据){
$(“#id_deficuldade”).empty();
$.each(数据、函数(索引、行){
$(“#id_deficuldade”).append(“+row.TIPO_deficuldade+”)
});
});
})
});
我的问题

我的问题是,该值在第二个组合框中返回
未定义的
,就好像该值未返回或未被识别一样。

您的
testeee()
方法返回的是一个
定义的
实例,而不是一个集合。更改方法以创建集合,并在读取数据时添加每个集合

public JsonResult Testeeee(int id_cat )
{
    List<dificuldade> model = new List<dificuldade>();
    ....
    SqlDataReader reader = com.ExecuteReader();
    while (reader.Read())
    {
        dificuldade item = new dificuldade();
        item.TIPO_DIFICULDADE = reader["tipo_dificuldade"].ToString();
        item.id_dificuldade = int.Parse(reader["id_dificuldade"].ToString());
        model.Add(item);
    }
    conn.Close();
    return Json(model, JsonRequestBehavior.AllowGet);
}
public JsonResult testeee(int-id\u-cat)
{
列表模型=新列表();
....
SqlDataReader=com.ExecuteReader();
while(reader.Read())
{
定义项=新定义项();
item.TIPO_deficuldade=读卡器[“TIPO_deficuldade”].ToString();
item.id_deficuldade=int.Parse(读卡器[“id_deficuldade”].ToString());
模型。添加(项目);
}
康涅狄格州关闭();
返回Json(model,JsonRequestBehavior.AllowGet);
}

作为补充说明,您还有其他问题,包括如果需要在POST方法中返回视图,则会丢失数据,如果编辑现有记录,代码将不会显示正确的数据。请参阅

您的控制器方法返回一个
dificuldade
,而不是一个集合(并且您不能迭代单个对象),如何使其作为列表返回?初始化
列表
,并使用
while(reader.Read())
向该集合添加
dificuldade
的新实例,然后返回集合你能用我的控制器做个例子吗?如果可以的话,我对MVC有点陌生。谢谢^-^它起作用了!非常感谢你,我现在明白为什么它不起作用了!!请不要对我们大喊大叫:)