2016-02-12 105 views
0

我有兩個ASP NET站點。他們有不同的邏輯和頁面,從不同的數據庫獲取/寫入數據。 我想創建一個大師佈局(與標題,菜單頁腳等),並加入這些網站,就像他們看起來像一個單一的網站。所以問題是可能的嗎? 謝謝。asp net mvc跨站點佈局

回答

0

你想實現在一個視圖中顯示不同模型的東西嗎?如果是,那麼你可以使用ViewModels。創建viewModel類,並在此處包括要顯示的模型。

public class ViewModel 
    { 
     public Book Book { get; set; } 
     public IEnumerable<Book> Books { get; set; } 

     public Genre Genre { get; set; } 
     public IEnumerable<Genre> Genres { get; set; } 
     //and so on... 
    } 

然後在您的操作方法中,您需要創建新的viewModel並將其傳遞給返回視圖。

var viewModel = new ViewModel 
       { 
        Books= db.Books.ToList(), 
        Genres = db.Genres.ToList() 
//you can also of course specify here a Book or Genre 
       }; 

return View(viewModel); 

在您的視圖/或佈局中指定您正在使用的模型。

@model YourProjectName.ViewModels.ViewModel 

現在你可以使用你在viewModel類指定的所有屬性。

更多視圖模型:

Way to Use Multiple Models in a view in ASP.NET MVC

What is ViewModel in MVC?