![](/img/trans.png)
[英]How to pass custom model object from razor view to controller action in ASP.NET MVC 5
[英]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中。
嘗試為此修改代碼
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.