简体   繁体   English

如何使用asp.net将对象从控制器传递到视图(电动机aspx不是剃须刀)

[英]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中。

  • Create a class with Topics as name, and set public properties in the Topics class. 创建一个以Topics为名称的类,并在Topics类中设置公共属性。
  • Create a strong type view with Topics as your model, within this view, you can display or edit your properties set in Topics class. 创建一个以Topics为模型的强类型视图,在该视图内,您可以显示或编辑Topics类中设置的属性。
  • When you return a view back just return the model, in this case it will be return view (Topics). 当您返回视图时,只需返回模型,在这种情况下,它将是返回视图(主题)。

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.

相关问题 如何将自定义模型对象从剃刀视图传递到ASP.NET MVC 5中的控制器操作 - How to pass custom model object from razor view to controller action in ASP.NET MVC 5 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC 从控制器传递对象集合到View Asp.Net MVC - Pass Collection of Object from Controller to View Asp.Net MVC 在ASP.NET MVC 5中将整个对象从视图传递到控制器 - Pass entire object from view to controller in ASP.NET MVC 5 将数据从视图传递到ASP.NET MVC4剃须刀中的控制器 - Pass data from view to controller in asp.net mvc4 razor 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? 如何将值从视图传递到Asp.net中的控制器 - How to pass values from view to controller in Asp.net 如何在asp.net中将值从控制器传递到视图? - How to pass values from controller to view in asp.net? 如何使用POST将复杂对象从视图传递到控制器-ASP.NET MVC - How can i pass complex object from view to controller with POST - ASP.NET MVC 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM