如何在jQuery.each()函数中获取子属性?

时间:2012-02-10 04:29:51

标签: c# jquery asp.net-mvc-3

在控制器中:

        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显示它。感谢帮助!

示例预览:

enter image description here

2 个答案:

答案 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);
}

此代码中可以改进的内容:

  • 不要使用字符串连接来生成DOM树。使用jQuery( html, props )重载。
  • 不要硬编码Details锚点的网址 - &gt;使用url helpers生成它。

答案 1 :(得分:1)

在您的控制器中尝试:

return Json(jsonData);

那将返回一个JsonResult。