user240934
user240934

Reputation: 15

MVC - Displaying Content

How to Display the following

     public ActionResult Index()
     {
        IEnumerable<int> items = Enumerable.Range(1000, 5);
        ViewData["Collection"] = items;
        return View();
    }

in "View"

<ul>
     <% foreach(int i in  (IEnumerable)ViewData["Collection"]){ %>
       <li>
             <% =i.ToString(); }%>
       </li>    
</ul>    

the foreach throws System.Web.HttpCompileException.

Upvotes: 0

Views: 72

Answers (1)

Eilon
Eilon

Reputation: 25704

You had the closing brace of the foreach's loop in the wrong place. This is what you need:

<ul> 
    <% foreach (int i in (IEnumerable)ViewData["Collection"]) { %>
    <li>
        <%= i.ToString() %>
    </li>
    <% } %>
</ul>

And you also had some other extra punctuation in there as well (such as an extra semicolon).

Upvotes: 4

Related Questions