使用 jquery.ajax 将复杂类型(多个列表)返回到客户端

发布于 2024-10-16 04:32:52 字数 501 浏览 3 评论 0原文

我正在设计一个页面,它对服务器端的页面方法进行 ajax 调用(通过 jQuery.ajax)。
在服务器端,我有两个类:AgentChannel
在页面方法中,我想向客户端返回一个 ListList
如何向客户端返回两个列表?应该将它们包装在一个类中,例如:

public class ReturnData
  {
     public List<Agent> Agents{ get; set; }
     public List<Channel> Channels{ get; set; }
  }  

或者有更好的方法吗?
另外,我如何在客户端访问这些列表的项目?类似 foreach(var item in Agents) 但在客户端?

I'm designing a page which makes an ajax call (via jQuery.ajax) to a page method on the server side.
On the server side, I have two classes: Agent and Channel.
In the page method, I'd like to return a List<Agent> and a List<Channel> to the client side.
How can I return two lists to client side? should wrap them up in one class like:

public class ReturnData
  {
     public List<Agent> Agents{ get; set; }
     public List<Channel> Channels{ get; set; }
  }  

Or is there a better way?
Also, How can I access to these lists' items in client side? Something like foreach(var item in Agents) but in client side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠佳期 2024-10-23 04:32:52

控制器代码

  public ActionResult SomeAction()
    {
        List<Agent> collectionOfAgents = //get your collection
        List<Channels> collectionOfChannels = //get your collection
        var jsonData = new
                           {
                                   Agents =collectionOfAgents,
                                   Channels = collectionOfChannels 

                           };
        return Json(jsonData);
    }

Jquery代码

                    jQuery.ajax({
                            url: '/Controller/SomeAction',
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                var collectionOfAgents = data.Agents;
                                //iterate over agents
                                for(var a in collectionOfAgents)
                                {
                                  alert(collectionOfAgents[a]);
                                }
                                var collectionOfChannels = data.Channels;
                                //iterate over channels 
                                for(var c in collectionOfChannels)
                                {
                                  alert(collectionOfChannels[c]);
                                }
                            }
                        });

Controller code

  public ActionResult SomeAction()
    {
        List<Agent> collectionOfAgents = //get your collection
        List<Channels> collectionOfChannels = //get your collection
        var jsonData = new
                           {
                                   Agents =collectionOfAgents,
                                   Channels = collectionOfChannels 

                           };
        return Json(jsonData);
    }

Jquery code

                    jQuery.ajax({
                            url: '/Controller/SomeAction',
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                var collectionOfAgents = data.Agents;
                                //iterate over agents
                                for(var a in collectionOfAgents)
                                {
                                  alert(collectionOfAgents[a]);
                                }
                                var collectionOfChannels = data.Channels;
                                //iterate over channels 
                                for(var c in collectionOfChannels)
                                {
                                  alert(collectionOfChannels[c]);
                                }
                            }
                        });
似梦非梦 2024-10-23 04:32:52

容器类工作得很好。通常,您最终希望在顶层也包含一些额外的项目,然后容器类就很有用了。

如果您不喜欢额外的类,您可以使用匿名类型来避免容器类:

public static object GetAgentsAndChannels() {
  List<Agent> agents = GetListOfAgents();
  List<Channel> channels = GetListOfChannels();

  return new { Agents: agents, Channels, channels };
}

在客户端,它们将作为常规数组出现。您可以像这样访问它们:

$.ajax({
  url: 'YourPage.aspx/GetAgentsAndChannels',
  type: 'POST',
  dataType: 'json',
  contentType: 'application/json',
  success: function(response) {
    var agentsAndChannels = response.d;

    var numberOfAgents = agentsAndChannels.Agents.length;
    var numberOfChannels = agentsAndChannels.Channels.length;

    var firstAgent = agentsAndChannels.Agents[0];

    var firstChannelName = agentsAndChannels.Channels[0].Name;

    // Etc.
  }
});

The container class works well enough. Often, you end up wanting to include a few extra items at the top level too, and then the container class is useful.

If you don't like the extra class, you can use an anonymous type to avoid the container class:

public static object GetAgentsAndChannels() {
  List<Agent> agents = GetListOfAgents();
  List<Channel> channels = GetListOfChannels();

  return new { Agents: agents, Channels, channels };
}

On the client-side, they'll be present as regular arrays. You can access them like this:

$.ajax({
  url: 'YourPage.aspx/GetAgentsAndChannels',
  type: 'POST',
  dataType: 'json',
  contentType: 'application/json',
  success: function(response) {
    var agentsAndChannels = response.d;

    var numberOfAgents = agentsAndChannels.Agents.length;
    var numberOfChannels = agentsAndChannels.Channels.length;

    var firstAgent = agentsAndChannels.Agents[0];

    var firstChannelName = agentsAndChannels.Channels[0].Name;

    // Etc.
  }
});
三岁铭 2024-10-23 04:32:52

正如 @igorw 所评论的,管理它的最佳方法是将它们包装在 JSON 对象中。最简单的方法是这样的...

return Json(new {agent = Agents, channel = Channels});

这会将代码以 javascript 友好的格式发送回来,并将两个列表分成两部分,这样您就可以通过 javascript 单步执行其中任何一个,并且仍然知道哪个一个是哪个。

As was commented by @igorw, the best way to manage it would be to wrap them up in a JSON object. Easiest way to do this would be something like...

return Json(new {agent = Agents, channel = Channels});

This sends you the code back in a javascript friendly format, and keeps the two lists split apart into 2 pieces, so you can step through either of them by javascript, and still know which one is which.

作死小能手 2024-10-23 04:32:52

您遗漏了 jQuery.ajax 调用的详细信息...如果您可以提供更多详细信息,您可能会找到更好的答案。

假设您发出 GET 请求,您可以将列表包装在 JSON 对象中。 JSON 序列化所需的 badnwidth 比 HTML/XML 序列化对应物少得多。然后,在客户端访问方面,您将处理对象文字:

{  
    Agents: [ {name:"Jane"}, {name: "John"} ],
    Channels: [ {someValue: 123}, {someValue: 456} ]
}

使用 JSON 结果,您可以使用标准 JavaScript for/in 语法来迭代代理和通道列表。

在不了解您的具体情况的情况下,不可能说是否有“更好”的方法,但听起来您已经走在正确的轨道上。

You left out the details of your jQuery.ajax call... you may find a better answer if you can provide more detail.

Assuming you're making a GET request, you can wrap your lists in a JSON object. JSON serialization will require far less badnwidth than HTML/XML serialization counterparts. Then, in terms of client-side access, you'll be dealing with an object literal:

{  
    Agents: [ {name:"Jane"}, {name: "John"} ],
    Channels: [ {someValue: 123}, {someValue: 456} ]
}

Using your JSON result you can use standard JavaScript for/in syntax to iterate over the Agents and Channels lists.

It's not possible to say whether there's a "better" approach without knowing the details of your situation, but it sounds like you're already on the right track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文