【发布时间】:2021-07-08 02:38:04
【问题描述】:
Please click here to see the debugged result and rendered data image
我正在一个 ASP.NET Core MVC 应用程序中开发一个函数,每次单击下拉列表项时,它都会根据项的 id 呈现相应的数据。
这是我在控制器中的操作
// this function get list of course and render a dropdown list of courses.
public async Task<IActionResult> Index()
{
List<SelectListItem> listItem = new List<SelectListItem>();
var courses = await _context.Course.ToListAsync();
foreach (var course in courses)
{
listItem.Add(new SelectListItem { Text = course.CourseName, Value = course.CourseId.ToString() });
}
ViewBag.CourseList = listItem;
return View();
}
这是一个将部分视图作为 JSON 数据返回的函数,具体取决于课程的 ID。
[HttpGet]
public async Task<IActionResult> GetNoteById(int id)
{
var noteModel = await _context.Course
.Include(n => n.Notices)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.CourseId == id);
if (noteModel == null)
return Json(new { data = NotFound(), message = "Error while retrieving data." });
return Json(noteModel);
}
这是我的.cshtml 文件
// some code ....
<div class="col-sm-4 offset-4 form-inline">
<label class="">Courses </label>
<select id="courses_dropdown_list" class="custom-select ml-2"
asp-items="@(new SelectList(ViewBag.CourseList, "Value","Text") )">
</select>
</div>
// some code ...
<table class="table table-striped table-bordered" style="width:100%;">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>File Type</th>
<th>File</th>
<th>Created On</th>
</tr>
</thead>
<tbody id="note_table">
// partial view will be loaded in here
</tbody>
</table>
最后,这是我的 ajax 脚本,可以帮助呈现 JSON 数据。
<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#courses_dropdown_list").change(function () {
var id = $("#courses_dropdown_list").val();
var noteTable = $("#note_table");
var result = "";
$.ajax({
cache: false,
type: "GET",
url: "@Url.Action("GetNoteById", "Note")",
datatype: "json",
data: { "id": id },
success: function (data) {
noteTable.html("");
$.each(data, function (id, note) {
/* this is what I tried to debug
there is no problem with the data
but when it is renderd, it returned undefined */
console.log(id);
console.log(note);
result += `
<tr>
<td>${note.Id}</td>
<td>
<a href="/note/NoteDetail/${note.Id}" class="nav-link mt-0">${note.Name}</a>
</td>
<td>
<a href="/note/DowloadNoteFromDB/${note.Id}" class="nav-link mt-0">###</a>
</td>
<td>{note.FileType}</td>
<td>{note.CreatedOn}</td>
</tr>`
});
noteTable.html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("failed to load data.");
}
});
});
});
当我单击下拉列表项时,表格按预期呈现。但是,每列中的数据返回为undefined。我尝试使用console.log(id) 和console.log(note) 进行调试,以检查在ajax 函数中传递的id 和note 是否正常。但是返回的数据是好的,没有问题。希望有人能帮帮我!
【问题讨论】:
-
GetNoteById操作仅返回单个对象noteModel而不是集合。但是您正试图在 AJAX 成功中循环通过它。因此,要么从操作方法返回一个集合,要么更改 AJAX 成功以将响应视为单个对象而不是集合。 -
@ChetanRanpariya 不,这个返回的模型包含另一个实例的集合。你可以看到我贴在帖子上的图片。
-
集合在
noteModel模型内。所以在 AJAX 成功时,data将是nodeModel。并访问您需要循环通过data.notices的集合 -
@ChetanRanpariya 如何在 ajax 函数中做到这一点?你能提供一个代码sn-p吗?提前致谢!
-
尝试将
$.each(data, function (id, note) {更改为$.each(data.notices, function (id, note) {
标签: c# ajax asp.net-core-mvc