Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Collection in VB ASP.NET MVC Razor

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
like image 790
user1477388 Avatar asked Jul 26 '12 17:07

user1477388


4 Answers

The answer is:

@For Each item In ViewBag.Articles
    @<div>@item.Title</div>
Next
like image 123
user1477388 Avatar answered Oct 31 '22 22:10

user1477388


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
like image 41
MikeTWebb Avatar answered Oct 31 '22 22:10

MikeTWebb


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
like image 1
Shyju Avatar answered Oct 31 '22 20:10

Shyju


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#

like image 1
KyleMit Avatar answered Oct 31 '22 20:10

KyleMit