开发者

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.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜