我從數據庫中檢索國家的名稱和ID並將其設置在Viewbag中。但是,當我從視圖訪問它的投擲錯誤。無法從Viewbag項目中檢索屬性
下面是C#代碼: -
var country = from x in db.Territories select new
{
x.Name,
x.ID
};
ViewBag.countries = country;
查看網頁代碼: -
@foreach (var item in ViewBag.countries)
{
<td><label class="control-label">@item.Name</label></td>
}
錯誤: -
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'
謝謝你..它的工作。:) – user3206357