---
title: "Pass an object from controller to view with asp.net"  
description: "Pass an object from controller to view with asp.net"  
author: "Anonymous User"  
published: 2015-01-29  
updated: 2015-01-29  
canonical: https://www.mindstick.com/forum/12918/pass-an-object-from-controller-to-view-with-asp-dot-net  
category: "asp.net"  
tags: ["c#", "asp.net mvc", "view", "controller"]  
reading_time: 1 minute  

---

# Pass an object from controller to view with asp.net

need to pass an object from my [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) to my [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view), I have the next [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)

```
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](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of [topics](https://yourviews.mindstick.com/story/2269/here-are-the-important-topics-in-the-history-and-culture-of-india-for-the-upsc-exam).Count in my view and putting it in a for.

## Replies

### Reply by Anonymous User

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

```
@for(var i=0;i<ViewBar.Counter;i++){  //Do your logic here }
```


---

Original Source: https://www.mindstick.com/forum/12918/pass-an-object-from-controller-to-view-with-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
