[英]How pass an object from controller to view with asp.net (motor aspx not razor)
I need to pass an object from my controller to my view, I have the next code 我需要将一个对象从控制器传递到我的视图,我有下一个代码
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);
}
and I need use the value of topics.Count
in my view and putting it in a for. 我需要使用
topics.Count
的值。将其计入我的视图并将其放在for中。
Try modifying your code to this 尝试为此修改代码
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);
}
And in your View add this if your are using Razor syntax 如果您使用的是Razor语法,请在视图中添加
@for(var i=0;i<ViewBar.Counter;i++){
//Do your logic here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.