我是asp.net mvc
的新用户并写下此代码:
@foreach (var item in ViewBag.MyList)
{
@Html.ActionLink(@item.BookName, "Item", new {parentPartId = 0})
}
但在这一部分:
@item.BookName
这是我的控制器代码:
List<StoreList> myList=new List<StoreList>();
foreach (var VARIABLE in query_BookItem)
{
StoreList myClass = new StoreList();
myClass.BookName = VARIABLE.BookName;
myClass.BookText = VARIABLE.BookText;
myList.Add(myClass);
}
ViewBag.MyList = myList;
答案 0 :(得分:1)
ViewBag
为dynamic
,您无法在dynamic
方法中使用HtmlHelper
。您需要先将项目转换为StoreList
(假设您在视图中包含@using
语句以指向StoreList
所在的程序集
@foreach (StoreList item in ViewBag.MyList)
{
@Html.ActionLink(item.BookName, "Item", new {parentPartId = 0})
}
或
@foreach (var item in ViewBag.MyList as List<StoreList>)
{
@Html.ActionLink(item.BookName, "Item", new {parentPartId = 0})
}
或
@foreach (var item in ViewBag.MyList)
{
@Html.ActionLink(((StoreList)item).BookName, "Item", new {parentPartId = 0})
}
答案 1 :(得分:0)
变化:
@Html.ActionLink(@item.BookName, "Item", new {parentPartId = 0})
对此并解决:
<a href="@Url.Action("Item", "Store", new {parentPartId = 0})">@item.BookName</a>