Loop through Collection in VB ASP.NET MVC Razor

asp.net-mvc, asp.net-mvc-3, vb.net

Solution

The answer is:

@For Each item In ViewBag.Articles
    @<div>@item.Title</div>
Next

Problem

I am using the following Razor script to loop through but it gives me the following error: ``` @foreach (var item in ViewBag.Articles) { <div>@item.Title</div> } ``` Error: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30451: 'foreach' is not declared. It may be inaccessible due to its protection level. Source Error: Line 29: Articles Line 30: Line 31: @foreach (var item in ViewBag.Articles) Line 32: { Line 33: @(item.index). @item.model.Description Source File: C:\Users\darchual\documents\visual studio 2010\Projects\Blog\Blog\Views\Blog\Details.vbhtml Line: 31 It also says in my IDE that "'foreach' is not declared. It may be inaccessible due to its protection level." How do I loop through the collection? Thank you for your help. Edit: Here is the whole code: @ModelType Blog.Blog @Code ViewData("Title") = ViewBag.Title End Code Details Blog ``` <div class="display-label">name</div> <div class="display-field"> @Html.DisplayFor(Function(model) model.name) </div> <div class="display-label">description</div> <div class="display-field"> @Html.DisplayFor(Function(model) model.description) </div> <div class="display-label">dateCreated</div> <div class="display-field"> @Html.DisplayFor(Function(model) model.dateCreated) </div> </fieldset> ``` Articles ``` @foreach (var item in ViewBag.Articles) { <div>@item.Title</div> } ``` ``` @Html.ActionLink("Edit", "Edit", New With {.id = Model.BlogId}) | @Html.ActionLink("Back to List", "Index") </p> ``` Here is the Blog object: Imports System.Data.Entity Imports System.ComponentModel.DataAnnotations Public Class Blog ``` Public Property BlogId() As Integer Public Property Name() As String Public Property Description() As String Public Property DateCreated As Date Public Overridable Property Articles() As ICollection(Of Article) ``` End Class Public Class BlogDbContext ``` Inherits DbContext Public Property Blogs As DbSet(Of Blog) ``` End Class Edit: Finally got it to work. Working code is: ``` @For Each item In ViewBag.Articles @<div>@item.Title</div> Next ```

Original source