2016-09-27 86 views
-2

我試圖從userList viewbag.i獲取值無法找出解決方案。錯誤是:無法從Viewbag中獲得foreach值mvc

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code

Additional information: 'object' does not contain a definition for 'name'

儘管在ViewBag.userList包含數據(2個對象),其在調試

@foreach (var aUser in ViewBag.userList) 
{ 
    <tr> 
     <td>@aUser.name</td> 
     <td>@aUser.username</td> 
     ..... 
     <td>@Html.ActionLink("Edit", "UserEdit","Users")</td> 
     <td>@Html.ActionLink("Delete", "UserDelete", "Users")</td> 
    </tr> 
} 

我可以看到我有一個超類和childclass

public partial class user 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string username { get; set; } 
    ... 
    public string user_role { get; set; } 
} 

孩子類

public class UserSub: user 
{ 
    public string CreatedUserName { get; set; } 
    public string ModifiedUserName { get; set; } 

} 

在我的控制器中,我使用linq從數據庫獲取值並將其存儲到Viewbag.userList中。我的控制器功能是

public ActionResult UserList() 
{ 
    IEnumerable<user> users = null; 
    users = dbEntities.users.ToList(); 

    if (users != null) 
    { 
     var userLists = (from a in users join b in users on a.created_user_id equals b.id select new { a.name, a.username, a.password, a.user_role, a.is_enable, a.is_approved, CreatedUserName = b.name, a.create_time, a.is_android, a.device_id }).ToList(); 
     ViewBag.userList = userLists; 
    } 
    return View(); 
} 

試圖List<UserSub> users=ViewBag.userList ....收到錯誤太

+2

因爲'ViewBag.userList'是一個匿名對象的集合。您需要將您的數據投影到模型中(包含屬性'name','username','password'等)。不要使用'ViewBag' - 將模型傳遞給視圖) –

+0

試圖轉換...但也有錯誤 –

+0

爲什麼你不能將它作爲Model返回視圖(userLists);'而不是在ViewBag – Vijai

回答

0

使用視圖模型共享視圖和控制器之間的數據。

例如,首先創建視圖模型

public class userViewModel{  
    public int id { get; set; } 
    public string name { get; set; } 
    public string username { get; set; } 
    public string user_role { get; set; } 
    public string CreatedUserName { get; set; } 
    public string ModifiedUserName { get; set; } 
    ... 
} 

你可以把你在你的視圖模型所需要的所有數據...那麼,我就建議你用你的模型創建一個類所有你需要的查詢(你必須調查怎麼做),但是你可以從你的控制器獲得查詢(如果你想的話)。

好,編輯您的控制器功能:

public ActionResult UserList() 
{ 
    List<userViewModel> userVM = new List<userViewModel>(); //Important! Don't return all the query, just the data that you need. 
    IEnumerable<user> users = null; 
    users = dbEntities.users.ToList(); 

    if (users != null) 
    { 
     var userLists = (from a in users join b in users on a.created_user_id equals b.id select new { a.name, a.username, a.password, a.user_role, a.is_enable, a.is_approved, CreatedUserName = b.name, a.create_time, a.is_android, a.device_id }).ToList(); //I'm going to suppose that your query is ok and you get all the data that you need... 

     foreach (var item in userLists) 
     { 
      userVM.Add(new userVM(){ 
       userVM.name = item.name; 
       userVM.username = item.username; 
       userVM.user_role = item.user_role; 
       ....... 
      });     
     }    
    } 
    return View(userVM); //return your view model 
} 

最後修改視圖並調用視圖模型userViewModel

@model Model.ViewModel.userViewModel //It depends on the namespace 
//Then try something likes this... 
@foreach (var aUser in Model) 
{ 
    <tr> 
     <td>@aUser.name</td>    
     <td>@aUser.username</td> 
     ..... 
     <td>@Html.ActionLink("Edit", "UserEdit","Users")</td> 
     <td>@Html.ActionLink("Delete", "UserDelete", "Users")</td> 
    </tr> 
} 

這就是理念,提高我的答案。 ;)

+1

只是一個小小的評論 - 你可能在你對謂詞(where子句)進行反擊之前,不想調用你的'dbEntities.users'上的'ToList()',否則你會從數據庫中選擇一切並過濾。而不是使用數據庫引擎進行過濾(假設適用) – Charleh