0

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.

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.