C# 如何使用asp.net将对象从控制器传递到视图(motor aspx而不是razor)

C# 如何使用asp.net将对象从控制器传递到视图(motor aspx而不是razor),c#,asp.net,view,controller,C#,Asp.net,View,Controller,我需要将一个对象从我的控制器传递到我的视图,我有下一个代码 public ActionResult General(int id) { List<Topics> topics = new List<Topics>(); Topics top = new Topics(); List<string> items = new List<string>(); topics = top

我需要将一个对象从我的控制器传递到我的视图,我有下一个代码

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);
    }
我需要使用主题的价值。在我的视图中计数,并将其放在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

}

你的看法如何?您可以在web.motor aspx上获得样品?不知道你的确切意思。旁注:没有必要给topics=newlist赋值;正如您立即覆盖它,但我假设您知道它,并且只是将随机行组合为示例,仍然可能是很好的ide来修复它。我的意思是motorengine aspx中的语法,当您使用razor引擎时,语法与aspx不同。但是如果我需要传递一个对象以查看,我如何才能做到这一点?尝试添加验证。将值传递给ViewBag.Counter后,需要检查对象的类型并强制转换它或尝试强制转换它。