2016-05-18 27 views
0

我有兩種模型,我需要在佈局頁面和用戶訪問的每個頁面中顯示數據。這兩個模型之間沒有任何關係,所以我不需要任何連接。向佈局頁面顯示兩個表的數據MVC 5

這是我的控制器

public ActionResult Index() 
    { 
     var notification = (from n in db.Notification 
          where n.NotificationIsSeen == true 
          select n); 



     var task = (from t in db.Task 
          where t.TaskIsSeen == true 
          select t); 

     return View();// I not sure how to return both of queries 
    } 

我還創建包含他們的模型,但我「不知道這是正確的方式

public class Layout 
{ 


    public Notification Notification { get; set; } 
    public Task Task { get; set; } 
} 

,並在我的頁面佈局

@model IEnumerable<MyprojectName.Models.Layout> 

//other code 
@foreach (var item in Model) 
{ 
    <li>@Html.DisplayFor(modelItem => item.Notification.NotificationSubject) </li>} 

//other code 
@foreach (var item in Model) 
{ 
    <li>@Html.DisplayFor(modelItem => item.Task.TaskSubject) 
    </li> 
    } 

我見過其他類似的問題,但他們使用連接表。 我需要一些幫助來返回兩個表的數據。預先感謝您

+2

您查詢返回的集合,所以你的觀點模型需求成爲公共類佈局{public IEnumerable Notifications {get;組; } publicIEnumerable 任務{get;組; }}'並將'Layout'的一個實例返回給視圖。 –

+0

對於你需要什麼有一些困惑。你的模型有一個'Notification'和'Task',但是在你的動作方法中,你得到了一個兩者的集合。您查看顯示它需要您的視圖模型的集合。你能否澄清什麼是預期的行爲?Stephen Muecke – Nkosi

+0

。你是什​​麼意思返回一個單一的佈局實例?謝謝 – touinta

回答

3

您的操作方法中的查詢都返回數據集合。爲了適應這種情況,您的視圖模型需要有兩個列表,並且需要看起來像這樣。你必須能夠將它們發送到視圖時,這些集合存儲在列表中:

public class Layout 
{ 
    public IEnumerable<Notification> Notifications { get; set; } 

    public IEnumerable<Task> Tasks { get; set; } 
} 

要填充這些列表更改代碼在你的操作方法了這一點。創建的Layout一個實例,填充兩個列表,然後發送實例的視圖:

public ActionResult Index() 
{ 
    Layout model = new Layout(); 

    model.Notifications = (from n in db.Notification 
          where n.NotificationIsSeen == true 
          select n); 

    model.Tasks = (from t in db.Task 
        where t.TaskIsSeen == true 
        select t); 

    return View(model); 
} 

你的觀點需要接受和實例Layout

@model MyprojectName.Models.Layout 

@foreach (var notification in Model.Notifications) 
{ 
    <div> 
      @notification.NotificationSubject 
    </div> 
} 

@foreach (var task in Model.Tasks) 
{ 
    <div> 
      @task.TaskSubject 
    </div> 
} 

我希望這有助於。

+0

謝謝你的作品!但我唯一的問題是,當我去到另一個頁面它給模型的錯誤。我該如何使用它以便在所有視圖中看到 – touinta

+1

向我解釋你正在嘗試做什麼?我想也許這可能是另一個問題。一次一個問題:) –

+0

好吧我的意思是我需要顯示這些數據到每個頁面不只是在索引。在我的佈局頁面中,我稱之爲佈局模型。但是當我點擊另一個頁面時,我得到錯誤「傳入字典的模型項目的類型是'System.Collections.Generic.List'1 [Myprojectname.Models.Notification]',但是這個字典需要一個類型的模型項目'Myprojectname.Models.Layout'。」 – touinta

-1

請聲明模型的列表輸入您的佈局模型

佈局模型

public class Layout 
{ 
public IEnumerable<Notification> Notifications { get; set; } 

public IEnumerable<Task> Tasks { get; set; } 
} 

控制器

public ActionResult Index() 
{ 
Layout model = new Layout(); 

model.Notifications = (from n in db.Notification 
         where n.NotificationIsSeen == true 
         select n); 

model.Tasks = (from t in db.Task 
       where t.TaskIsSeen == true 
       select t); 

    return View(model); 
} 

查看

@model MyprojectName.Models.Layout 

@foreach(var item in Model.Notifications) 
{ 
// access your item.propertyname  
} 


@foreach(var item in Model.Task) 
{ 
// access your item.propertyname 
} 
相關問題