级联下拉 NET CORE
Cascade Dropdown NET CORE
我有点头疼,因为我不知道问题出在哪里。我有以下控制器:
public JsonResult GetActivitySubType(int typeId)
{
var data = (from a in _db.ActivitySubTypes
where a.ActivityTypeId == typeId
select new
{
a.ActivitySubTypeId, a.ActivitySubTypeName
}
).ToList();
return Json(data);
}
public IActionResult Index()
{
ViewData["ActivityTypeId"] = new SelectList(_db.ActivityTypes.OrderBy(o => o.ActivityTypeName), "ActivityTypeId", "ActivityTypeCode");
return View();
}
然后在我看来。我有以下代码:
<select asp-for="ActivityTypeId" asp-items="ViewBag.ActivityTypeId"><option value="">Select</option></select>
<select id="ActivitySubTypeId" asp-for="ActivitySubTypeId"></select>
<script type="text/javascript">
$(document).ready(function () {
$("#ActivityTypeId").change(function () {
$.get("GetActivitySubType", { typeId: $("#ActivityTypeId").val() }, function (data) {
$("#ActivitySubTypeId").empty();
$.each(data, function (index, row) {
$("#ActivitySubTypeId").append("<option value='" + row.ActivitySubTypeId + "'>" + row.ActivitySubTypeName + "</option>")
});
});
})
});
</script>
当我 select ActivityType
时,它在我的 select ActivitySubType
上加载了 undefined
。我错过了什么吗?
非常感谢。
asp.net 核心中响应的命名约定是 camel case
而不是 pascal case
。
更改如下:
$("#ActivitySubTypeId").append("<option value='" + row.activitySubTypeId + "'>" + row.activitySubTypeName + "</option>")
我有点头疼,因为我不知道问题出在哪里。我有以下控制器:
public JsonResult GetActivitySubType(int typeId)
{
var data = (from a in _db.ActivitySubTypes
where a.ActivityTypeId == typeId
select new
{
a.ActivitySubTypeId, a.ActivitySubTypeName
}
).ToList();
return Json(data);
}
public IActionResult Index()
{
ViewData["ActivityTypeId"] = new SelectList(_db.ActivityTypes.OrderBy(o => o.ActivityTypeName), "ActivityTypeId", "ActivityTypeCode");
return View();
}
然后在我看来。我有以下代码:
<select asp-for="ActivityTypeId" asp-items="ViewBag.ActivityTypeId"><option value="">Select</option></select>
<select id="ActivitySubTypeId" asp-for="ActivitySubTypeId"></select>
<script type="text/javascript">
$(document).ready(function () {
$("#ActivityTypeId").change(function () {
$.get("GetActivitySubType", { typeId: $("#ActivityTypeId").val() }, function (data) {
$("#ActivitySubTypeId").empty();
$.each(data, function (index, row) {
$("#ActivitySubTypeId").append("<option value='" + row.ActivitySubTypeId + "'>" + row.ActivitySubTypeName + "</option>")
});
});
})
});
</script>
当我 select ActivityType
时,它在我的 select ActivitySubType
上加载了 undefined
。我错过了什么吗?
非常感谢。
asp.net 核心中响应的命名约定是 camel case
而不是 pascal case
。
更改如下:
$("#ActivitySubTypeId").append("<option value='" + row.activitySubTypeId + "'>" + row.activitySubTypeName + "</option>")