5
votes

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
4
Omg, let me get this right, your mistake was using the wrong programming language? Holly shivers that is a classic! I'm going to remember that one L-O-L :) - Erx_VB.NExT.Coder
Lol it was a simple mistake to make :) - user1477388
One of the Top 10 signs that you are using too many different types of programming languages in any 24 hour period. :) - Erx_VB.NExT.Coder

4 Answers

7
votes

The answer is:

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

Your page code is in VB.Net and foreach() is a C# construct. You just need to modify your code use the VB Construct of the For Each loop:

This thread on the ASP.NET Forum's has a good answer / code snippet:

Dim list As New List(Of Article)
list = ViewBag.Articles
If (list.Any()) 
Then     
    For Each item As Article In ViewBag.Articles 
        <div>@item.Title</div>
    Next
End If
1
votes

If you are already in a razor code block, you do not need the @

@if(ViewBag.Articles.Count>0)
{
   foreach (var item in ViewBag.Articles)
   { 
     <div>@item.Title</div>
   }
}

Are you using the VB.NET version of foreach ?

@For Each item As Article In ViewBag.Articles
  <div>@item.Title</div>
Next
1
votes

Because of the way the model binder works in ASP.NET MVC, there are a lot of scenarios where it's helpful to postback the index of each value in a list.

So I prefer to use an indexed for loop (over the simplicity of a strongly typed for each loop):

@For i = 0 To Model.Articles.Count - 1
    Dim curItem = Model.Articles(i)
    @Html.EditorFor(Function(model) curItem)
Next

Further Reading:
Introduction to ASP.NET Web Programming Using the Razor Syntax (Visual Basic) | C#