开发者

MVC - Displaying Content

开发者 https://www.devze.com 2022-12-15 07:00 出处:网络
How to Display the following public ActionResult Index() { IEnumerable<int> items = Enumerable.Range(1000, 5);

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消