asp.net MVC3返回多个JSON列表 [英] asp.net mvc3 return multiple json list

查看:334
本文介绍了asp.net MVC3返回多个JSON列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林asp.net MVC3 C#code返回json的列表是这样的:

Im asp.net mvc3 c# code returns json list like this:

return Json(new { name = UserNames, imageUrl = ImageUrls });

用户名 ImageUrls 都是名单,其中,串> 类型

这是我的JavaScript

And this is my javascript

function StartSearch(text) {
    $.ajax({
        url: '/Shared/Search',
        type: 'POST',
        data: { SearchText: text },
        dataType: 'json',
        success: function (result) {
            $.each(result, function (i, item) {
                alert(result[i].name);
            });
        }
    });
}

我怎样才能得到名称和 ImageUrls

感谢

推荐答案

这提醒了名称从每个列表中的每个记录的值。

This alerts the value of Name from each record of each list.

$.each(result, function (i, item) {
    for (var x = 0; x < result.FirstList.length; x++) {
        alert(result.FirstList[x].Name);
        alert(result.SecondList[x].Name);
    }
});

此假定您的JSON响应,如果形成正确。像这样的:

This assumes your Json response if formed correctly. Like this:

return Json(new { FirstList = results, SecondList = otherResults }, JsonRequestBehavior.AllowGet);


但作为一个方面说明,我看其他问题,您的code,你需要解决


But as a side note, I see other problems with your code that you need to address

  1. 您实际上是在不执行 POST ,您正在搜索的基础上的输入。更改 POST GET 在Ajax调用
  2. 在改变你的行动回流管道,允许GET,并确保您正在返回一个 JsonResult
  3. 命名约定C#方法参数要求帕斯卡 - 套管。使用小写字母的第一个字符

  1. You're actually not performing a POST, you're searching based on input. Change POST to GET in your Ajax call
  2. Change your action return line to allow for the get and make sure your are returning a JsonResult.
  3. Naming conventions for C# method parameters call for Pascal-casing. Use a lowercase letter for first character

public JsonResult Search(string searchText) {
    ....
    return Json(new { name = UserNames, imageUrl = ImageUrls }, JsonRequestBehavior.AllowGet);
}

这篇关于asp.net MVC3返回多个JSON列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆