【发布时间】:2020-05-03 17:14:53
【问题描述】:
我试图通过 Ajax 将列表作为 json 字符串从控制器传递到视图,并希望以表格格式显示 json 字符串,但我只是在视图中得到普通的 json 字符串。
控制器代码
[HttpGet]
public JsonResult GetRegRequests()
{
var model = new TaskManagementEntities().RegRequests.ToList();
return Json(model,JsonRequestBehavior.AllowGet);
}
这是查看代码。
<div id="showRequest">
<table class="table-responsive reqtab">
<thead class="thead-dark">
<th>Req ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Password</th>
</thead>
<tbody>
</tbody>
</table>
</div>
和脚本标签
<script>
$.ajax({
cache: false,
type: 'GET',
url: @Url.Action("MasterAdmin", "Index"),
dataType: 'json',
success:
function (result) {
console.log(result);
$(".reqtab tbody").append("<tr><td>" + result.first_name + "</td><td>" + result.last_name + "</td><td>" + result.user_name +"</td><td>"+ result.password+"</td></tr>")
}
})
</script>
我也没有得到表头,而是得到了纯 json 数据,浏览器控制台显示以下错误。我不确定这个错误是否相关。
jquery.unobtrusive-ajax.min.js:16 Uncaught ReferenceError: jQuery is not defined
at jquery.unobtrusive-ajax.min.js:16
如何将这个 json 字符串传递给 View 并使用它来形成表格?
【问题讨论】:
-
你应该在 jquery.unobtrusive 之前加载 jquery.js。
标签: json ajax asp.net-mvc asp.net-ajax