2016-05-18 12 views
0

2つのモデルがあり、レイアウトページとユーザーが訪れるすべてのページにデータを表示する必要があります。これらの2つのモデルはそれらの間に関係がないので、私はどんな結合も必要ありません。2つの表のデータをレイアウトページに表示MVC 5

これは、私はまた、それらの両方が含まれていますが、これは正しい方法

public class Layout 
{ 


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

と私のレイアウトページ

である場合、私はわからない」モデルを作成し、私のコントローラ

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 
    } 

です

@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 class Layout {パブリックIEnumerable <通知>通知{取得;セット; } publicIEnumerable タスク{get;セット; }} 'を呼び出し、' Layout'の単一のインスタンスをビューに返します。 –

+0

必要なものについては混乱があります。あなたのモデルは単一の 'Notification'と' Task'を持っていますが、あなたのアクションメソッドには両方のコレクションがあります。あなたはviewmodelのコレクションが必要であることを表示します。あなたは意図された行動が何であるかを明確にすることができます – Nkosi

+0

Stephen Muecke。 Layoutのインスタンスを返すのはどういう意味ですか?ありがとうございました – touinta

答えて

3

あなたのアクションメソッドのクエリは両方ともデータのコレクションを返します。これに対応するには、ビューモデルに2つのリストが必要で、このようなものを見る必要があります。あなたは、ビューにそれらを送信するときにリストにこれらのコレクションを保存できるようにする必要があります:

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

あなたに何をしようとしていることを説明してください?これは別の質問かもしれないと思います。一度に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 
} 
関連する問題