use*_*388 4 vb.net asp.net-mvc asp.net-mvc-3
我使用以下Razor脚本循环但它给我以下错误:
@foreach (var item in ViewBag.Articles)
{
<div>@item.Title</div>
}
Run Code Online (Sandbox Code Playgroud)
错误:
描述:编译服务此请求所需的资源时发生错误.请查看以下特定错误详细信息并相应地修改源代码.
编译器错误消息:BC30451:未声明'foreach'.由于其保护级别,它可能无法访问.
来源错误:
第29行:文章第30行:第31行:@foreach(ViewBag.Articles中的var项)第32行:{第33行:
@(item.index).@ item.model.Description源文件:C:\ Users\darchual\documents\visual studio 2010\Projects\Blog\Blog\Views\Blog\Details.vbhtml Line:31
它还在我的IDE中说"'foreach'未被声明.由于其保护级别,它可能无法访问."
如何循环收集?谢谢您的帮助.
编辑:
这是整个代码:
@ModelType Blog.Blog
@Code ViewData("Title")= ViewBag.Title结束代码
细节
博客
Run Code Online (Sandbox Code Playgroud)<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>用品
Run Code Online (Sandbox Code Playgroud)@foreach (var item in ViewBag.Articles) { <div>@item.Title</div> }
Run Code Online (Sandbox Code Playgroud)@Html.ActionLink("Edit", "Edit", New With {.id = Model.BlogId}) | @Html.ActionLink("Back to List", "Index") </p>
这是Blog对象:
Imports System.Data.Entity Imports System.ComponentModel.DataAnnotations
公共课博客
Run Code Online (Sandbox Code Playgroud)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)结束班
公共类BlogDbContext
Run Code Online (Sandbox Code Playgroud)Inherits DbContext Public Property Blogs As DbSet(Of Blog)结束班
编辑:
终于搞定了.工作代码是:
@For Each item In ViewBag.Articles
@<div>@item.Title</div>
Next
Run Code Online (Sandbox Code Playgroud)
答案是:
@For Each item In ViewBag.Articles
@<div>@item.Title</div>
Next
Run Code Online (Sandbox Code Playgroud)