簡體   English   中英

如何使用asp.net將對象從控制器傳遞到視圖(電動機aspx不是剃須刀)

[英]How pass an object from controller to view with asp.net (motor aspx not razor)

我需要將一個對象從控制器傳遞到我的視圖,我有下一個代碼

public ActionResult General(int id)
    {
        List<Topics> topics = new List<Topics>();
        Topics top = new Topics();
        List<string> items = new List<string>();
        topics = top.getAllTopics(id);
        for (int i = 0; i < topics.Count; i++)
        {

            items.Add(topics[i].name);

        }
        ViewBag.Items = items; 
        return View(topics.Count);
    }

我需要使用topics.Count的值。將其計入我的視圖並將其放在for中。

  • 創建一個以Topics為名稱的類,並在Topics類中設置公共屬性。
  • 創建一個以Topics為模型的強類型視圖,在該視圖內,您可以顯示或編輯Topics類中設置的屬性。
  • 當您返回視圖時,只需返回模型,在這種情況下,它將是返回視圖(主題)。

嘗試為此修改代碼

public ActionResult General(int id)
    {
        List<Topics> topics = new List<Topics>();
        Topics top = new Topics();
        List<string> items = new List<string>();
        topics = top.getAllTopics(id);
        for (int i = 0; i < topics.Count; i++)
        {

            items.Add(topics[i].name);

        }
        ViewBag.Items = items;
        ViewBag.Counter = topics.Count; // this line was added
        return View(topics.Count);
    }

如果您使用的是Razor語法,請在視圖中添加

@for(var i=0;i<ViewBar.Counter;i++){
  //Do your logic here

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM