- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试根据上面的 ComboBox 的选择刷新我的 ComboBox。我有以下 Controller 代码:
根据第一个组合框的选择填充第二个组合框的 Controller 方法代码
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);
}
这是我的 View 代码:
@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>")
});
});
})
});
我的问题
我的问题是该值在第二个 ComboBox 中返回 undefined
,就好像该值没有返回或未被识别一样。
最佳答案
您的 Testeeee()
方法返回的是 dificuldade
的单个实例,而不是集合。更改创建集合的方法,在读取数据时添加每个
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);
}
作为旁注,您还有其他问题,包括如果您需要在 POST 方法中返回 View ,您将丢失数据,并且如果编辑现有记录,您的代码将不会显示正确的数据。引用How to keep cascade dropdownlist selected items after form submit?
关于c# - ASP.NET MVC 级联组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929628/
我以前使用过像 Netbeans 和 eclipse 这样的 IDE。 我在 friend 的电脑上下载了“Visual Studio Express 2013 for windows desktop
我正在尝试弄清楚如何在 GBA 大小的 EZ Flash 3 合 1 卡中对 PSRAM 进行编程。基本上重复 GBA Exploader 和其他程序所做的事情。 如果我选择一个 block 并对其进
Filter1=re.findall(r'',PageSource) Filter2=re.findall(r'',PageSource) Filter3=re.findall(r'(.*?).*?'
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我是一名优秀的程序员,十分优秀!