简体   繁体   English

如何在C#中的Dictionary数组中发送不同类的列表

[英]how to send the list of different class in Dictionary array in c#

I am developing a project in C# MVC.net. 我正在C#MVC.net中开发一个项目。

I have two lists in the controller 我在控制器中有两个列表

List<layouts> L1 = (List<layouts>)result.Data;
List<SeatPlans>[] allUser = new List<SeatPlans>[10];

and I am sending the allUser list to ajax as follows 并且我将allUser列表发送到ajax如下

var v = new Dictionary<String, List<SeatPlans>>();

int i;
for (i=0; i < L1.Count; i++)
{
    int k = i - 1;
    v.Add($"all{i}", allUser[i]);
    // id++;
}

return Json(v, JsonRequestBehavior.AllowGet);

and this much is working fine. 这样很好。 What I need is, have to send the List L1 also with allUser. 我需要的是,还必须与allUser发送列表L1。 How can I do this? 我怎样才能做到这一点? allUser is lists of SeatPlans class and L1 is the list of layouts class allUser是SeatPlans类的列表,L1是布局类的列表

create a class with two properties, one for each list 创建一个具有两个属性的类,每个属性一个

public class MyData
{
    public List<layouts> Layouts{get; set;}
    public List<SeatPlans> SeatPlans{get; set;}
}

and return an instance of that: 并返回一个实例:

return new MyData
{
    Layouts = L1,
    SeatPlans = allUser
};

in fact, you can actually avoid writing the definition for the class and return an anonymous type: 实际上,您实际上可以避免编写该类的定义并返回匿名类型:

return new
{
    Layouts = L1,
    SeatPlans = allUser
};

Do it like this. 像这样做。

Send a JsonResult back and create an instance in your controller: 发回一个JsonResult并在您的控制器中创建一个实例:

public JsonResult Action() {
    var json = new JsonResult();

    json.Body = new {
        List1 = L1, Dictionary = v
    };

    return json;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM