[英]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.