c# - 使用 getJSON 显示数组

标签 c# jquery asp.net asp.net-mvc ajax

所以我的代码中有一个函数,我正在使用 jQuery 的 getJSON 调用它。这是函数:

public JsonResult GetItems()
{
    var items = (from x in GetAllItems()
                 select new { x.ItemID, x.ItemType.Name, x.Description })
                .ToList();


    JsonResult result = Json(items, JsonRequestBehavior.AllowGet);

    return Json(result, JsonRequestBehavior.AllowGet);
}

这就是我用 jQuery 调用它的地方:

$.getJSON('/MyController/GetItems/', function (data) {
    // somehow iterate through items, displaying ItemID, Name, and Description
});

但我被困在这里,我不知道该怎么办。我想遍历每个项目,并在警告框中显示 ItemID、名称和描述。但是我发现的每个示例都只是展示了如何迭代和显示只有一个和一个的项目,但我的项目有两个以上的属性要显示。

最佳答案

试试这个:

$.getJSON('/MyController/GetItems/', function (data) {
    var name, itemId, description;
    $.each(data, function(){
         name = this.Name;
         itemId = this.ItemID;
         description = this.Description ;
         //You can use these variables and display them as per the need.  
    });
});

关于c# - 使用 getJSON 显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7002913/

相关文章:

c# - 创建一个普通数组,其中一个位置可以保存多个值

javascript - 动画递增数字并显示十进制值

javascript for循环只上传一个文件

c# - 显示日期时如何停止在 GridView 中显示时间?

c# - 从母版代码隐藏的子页面获取信息

c# - 如何使用 Azure.Data.Tables 列出表(按前缀)?

c# - 为什么一个别名的 C# 类型不能被另一个访问?

c# - 从函数返回事件

javascript - 我如何更新来自差异 socket.io 事件的消息?

asp.net - 它有一个 DefiningQuery 但没有 InsertFunction 元素...错误