在控制器中:
var jsonData = new
{
total = totalPages,
page = pageIndex,
records = totalRecords,
rows = founduser
};
return jsonData;
在视图中:
function listItems(col, order, page, rows) {
$.ajax({
type: "GET",
url: '@Url.Content("~/User/GetUsers")',
data: { col: col, order: order, page: page, rows: rows },
dataType: 'json',
success: function (response) {
var tags = "<tr><th>Login id</th><th>First name</th><th>Middle name</th><th>Last name</th><th>Prefix</th><th>Suffix</th><th>Reset</th><th>Email</th><th></th></tr>";
$.each(response, function (index, item) {
tags += "<tr id=row-" + item.UserID + ">" +
"<td>" + item.LoginID + "</td>" +
"<td>" + item.FirstName + "</td>" +
"<td>" + item.MiddleName + "</td>" +
"<td>" + item.LastName + "</td>" +
"<td>" + item.Prefix + "</td>" +
"<td>" + item.Suffix + "</td>" +
"<td>" + item.IsReset + "</td>" +
"<td>" + item.SendEmail + "</td>" +
"<td>" +
"<a href=# class='ActivateItem' data-id = " + item.UserID + " item-data = " + item.LoginID + ">Activate</a> | " +
"<a href=/BaseAdmin/User/Details/" + item.UserID + ">Details</a></td></tr>";
});
$("#table-list").html(tags);
}
});
};
行是IEnumerable array
,现在我怎么能得到行的值并放在表上?
<table id="table-list">
</table>
我能够检索值,但我无法使用jQuery.each显示它。感谢帮助!
示例预览:
答案 0 :(得分:3)
您从控制器操作返回JSON结果:
public ActionResult Foo()
{
var jsonData = new
{
total = totalPages,
page = pageIndex,
records = totalRecords,
rows = founduser
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
您可以遍历AJAX rows
回调中的success
属性:
success: function (response) {
var tags = "<tr><th>Login id</th><th>First name</th><th>Middle name</th><th>Last name</th><th>Prefix</th><th>Suffix</th><th>Reset</th><th>Email</th><th></th></tr>";
$.each(response.rows, function (index, item) {
tags += "<tr id=row-" + item.UserID + ">" +
"<td>" + item.LoginID + "</td>" +
"<td>" + item.FirstName + "</td>" +
"<td>" + item.MiddleName + "</td>" +
"<td>" + item.LastName + "</td>" +
"<td>" + item.Prefix + "</td>" +
"<td>" + item.Suffix + "</td>" +
"<td>" + item.IsReset + "</td>" +
"<td>" + item.SendEmail + "</td>" +
"<td>" +
"<a href=# class='ActivateItem' data-id = " + item.UserID + " item-data = " + item.LoginID + ">Activate</a> | " +
"<a href=/BaseAdmin/User/Details/" + item.UserID + ">Details</a>" +
"</td>" +
"</tr>";
});
$("#table-list").html(tags);
}
此代码中可以改进的内容:
jQuery( html, props )
重载。Details
锚点的网址 - &gt;使用url helpers生成它。答案 1 :(得分:1)
在您的控制器中尝试:
return Json(jsonData);
那将返回一个JsonResult。