简体   繁体   English

如何从控制器传递对象列表以进行查看?

[英]How do I pass a list of objects from controller to view?

I have some data (a list of datacontracts) in my controller. 我的控制器中有一些数据(数据合同列表)。 I want to show the fields/ values of that list inside my view. 我想在我的视图中显示该列表的字段/值。 How do I do this properly? 如何正确执行此操作?

This is the code inside my controller where I get the list data: 这是我的控制器中获取列表数据的代码:

public List<ShipmentDataContract> Get(long shipmentServiceId)
        {
            Logging.Instance.Info($"Shipment request came in for shipment with ShipmentServiceId = {shipmentServiceId}");
            ShipmentQueryResult shipmentQueryResult = GetShipmentByShipmentServiceIdResult(shipmentServiceId);
            Logging.Instance.Debug($"Shipment queried with ShipmentServiceId = {shipmentServiceId}");
            List<ShipmentDataContract> shipmentDataContracts = GetShipmentsFromResult(shipmentQueryResult);
            Logging.Instance.Info($"Shipment retrieved for shipment with ShipmentServiceId = {shipmentServiceId}.");
            return shipmentDataContracts;
        }

This method returns a list of datacontracts (only one in this case). 此方法返回一个数据合同列表(在这种情况下只有一个)。

I made a test method inside the same controller as well within the Index method: 我在同一控制器内以及在Index方法内都做了一个测试方法:

public ActionResult Index()
        {
            var shipmentDataTest = Get(94);

            ViewBag.shipmentTestData = shipmentDataTest;

            return View();
        }

When I debug the backend, it returns the right shipment (with ID 94). 当我调试后端时,它将返回正确的货件(ID为94)。

Now I want to show the shipment information within my view. 现在,我想在视图中显示货运信息。

I made a variabele in my view: 我认为我做了水痘:

<script>
    var shipmentTestData = '@ViewBag.shipmentTestData';
</script>

And within my Vue app file assigned the right values: 在我的Vue应用程序文件中,分配了正确的值:

var Vue = new Vue({
    el: "#vueapp",
    components: ["error"],
    data: {
        shipmentTestData: shipmentTestData
    }
});

Than when I call the data, it will not show the values, but a generic string. 比起我调用数据时,它不会显示值,而是一个通用字符串。

<p>{{ shipmentTestData }}</p>

It returns this: 它返回以下内容:

System.Collections.Generic.List`1[ShipmentDataContract]

Does someone know how to get this fixed? 有人知道如何解决此问题吗? For some reason the variable that I assign is of format string which causes this issue I assume but how can I fix this? 由于某种原因,我分配的变量具有格式字符串,这会导致我认为这个问题,但是我该如何解决呢?

This is the proper way. 这是正确的方法。

Model: 模型:

   public class ShipmentData
   {
       Task<List<ShipmentDataContract>> Get(long shipmentServiceId)
       {
          return YourList;
       }

Controller: 控制器:

  public ActionResult Index()
    {
        var shipmentDataTest = ShipmentData.Get(//index);

        return View(shipmentDataTest);
    }

View: 视图:

    @Model YourShipmentModel
    //here you can call Model.variable

If you want to use the list as string you can send the result like 如果要使用列表作为字符串,则可以将结果发送为

 IList<string> someStrings = Get(94); // i assume than get method returns a list of "strings"
 string joined = string.Join(",", someStrings );
 ViewBag.shipmentTestData = joined;

Now you are sending to frotend the list "shipmentDataTest" as a comma separated list. 现在,您要发送待发送列表“ shipmentDataTest”作为逗号分隔的列表。 Let me know if this is what you are looking for. 让我知道这是否是您想要的。 Greetings. 问候。

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

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