亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將對象列表從控制器傳遞給視圖?

如何將對象列表從控制器傳遞給視圖?

C#
holdtom 2022-12-24 14:13:14
我的控制器中有一些數據(數據合同列表)。我想在我的視圖中顯示該列表的字段/值。我該如何正確地做到這一點?這是我的控制器中獲取列表數據的代碼: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;        }此方法返回數據合同列表(在本例中只有一個)。我在同一個控制器內以及 Index 方法內創建了一個測試方法:public ActionResult Index()        {            var shipmentDataTest = Get(94);            ViewBag.shipmentTestData = shipmentDataTest;            return View();        }當我調試后端時,它返回正確的裝運(ID 為 94)?,F在我想在我的視圖中顯示裝運信息。在我看來,我做了一個變量:<script>    var shipmentTestData = '@ViewBag.shipmentTestData';</script>在我的 Vue 應用程序文件中分配了正確的值:var Vue = new Vue({    el: "#vueapp",    components: ["error"],    data: {        shipmentTestData: shipmentTestData    }});比起我調用數據時,它不會顯示值,而是顯示通用字符串。<p>{{ shipmentTestData }}</p>它返回這個:System.Collections.Generic.List`1[ShipmentDataContract]有人知道如何解決這個問題嗎?出于某種原因,我分配的變量是格式字符串,這導致了我假設的這個問題,但我該如何解決這個問題?
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

這是正確的方法。


模型:


   public class ShipmentData

   {

       Task<List<ShipmentDataContract>> Get(long shipmentServiceId)

       {

          return YourList;

       }

控制器:


  public ActionResult Index()

    {

        var shipmentDataTest = ShipmentData.Get(//index);


        return View(shipmentDataTest);

    }

看法:


    @Model YourShipmentModel

    //here you can call Model.variable


查看完整回答
反對 回復 2022-12-24
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

如果您想將列表用作字符串,您可以發送結果


 IList<string> someStrings = Get(94); // i assume than get method returns a list of "strings"

 string joined = string.Join(",", someStrings );

 ViewBag.shipmentTestData = joined;

現在您要將列表“shipmentDataTest”作為逗號分隔列表發送給前端。讓我知道這是否是您要找的東西。問候。


查看完整回答
反對 回復 2022-12-24
  • 2 回答
  • 0 關注
  • 90 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號