2013-07-15 88 views
0

我使用ASP.NET MVC 4傳遞給視圖

我有這個類:

namespace Attempt4.Models 
{ 
    public class UsersModel : DbContext 
    { 
     public UsersModel() 
      : base("name=UsersConnection") 
     { 
     } 
     public DbSet<UserProfile> UserProfiles { get; set; } 
     public DbSet<Roles> UserRoles { get; set; } 
     public DbSet<UsersInRoles> UsersInUserRoles { get; set; } 
    } 
} 

[Table("UserProfile")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; }  
} 

然後是另一個等級:

public partial class FskWebInterfaceContext : DbContext 
{ 
    public FskWebInterfaceContext() 
     : base("name=FskWebInterfaceContext") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     throw new UnintentionalCodeFirstException(); 
    } 

    public DbSet<trigger_dest_assoc> trigger_dest_assoc { get; set; } 
    public DbSet<ut_AccessLevel> ut_AccessLevel { get; set; } 
    public DbSet<ut_Client> ut_Client { get; set; } 
    public DbSet<ut_ContactID> ut_ContactID { get; set; } 
    public DbSet<ut_destinations> ut_destinations { get; set; } 
    public DbSet<ut_DeviceDescription> ut_DeviceDescription { get; set; } 
    public DbSet<ut_DeviceType> ut_DeviceType { get; set; } 
    public DbSet<ut_event_log> ut_event_log { get; set; } 
    public DbSet<ut_GMUTempData> ut_GMUTempData { get; set; } 
    public DbSet<ut_Triggers> ut_Triggers { get; set; } 
    public DbSet<ut_User> ut_User { get; set; } 
    public DbSet<ut_UserAPNdevices> ut_UserAPNdevices { get; set; } 
    public DbSet<ut_UserClientLink> ut_UserClientLink { get; set; } 
} 

現在我需要成爲一名可以從我的視圖中訪問這兩個數據庫上下文。 我知道如何通過一個模型,例如UserProfile。但是我需要能夠訪問這兩個類中的所有元素。

我該如何將它們從控制器傳遞給視圖。

而且,具體來說,一旦我通過它們,我如何在視圖中單獨訪問它們?

+0

從我一直在讀我需要使用的ViewModel類。 – Zapnologica

回答

2

您的問題的評論部分答案:

從我一直在讀什麼,我需要利用一個ViewModel類的。

因此,請繼續並定義一個包含必要信息的類。然後在你的控制器動作中填充這個模型的屬性,並讓它傳遞給視圖。

例如,讓我們假設你想從第一上下文和ut_GMUTempData從第二上下文訪問UserProfiles

public class MyViewModel 
{ 
    public IList<UserProfile> UserProfiles { get; set; } 
    public IList<ut_GMUTempData> GMUTempData { get; set; } 
} 

,並在你的控制器動作:

public ActionResult Index() 
{ 
    using (var ctx1 = new UsersModel()) 
    using (var ctx2 = new FskWebInterfaceContext()) 
    { 
     var model = new MyViewModel(); 
     model.UserProfiles = ctx1.UserProfiles.ToList(); 
     model.GMUTempData = ctx2.ut_GMUTempData.ToList(); 
     return View(model); 
    } 
} 

,現在您的看法變爲強類型的視圖模型,您可以訪問這兩個屬性:

@model MyViewModel 
... you could use both @Model.UserProfiles and @Model.GMUTempData collections 

UPDATE:

如這裏的註釋部分要求。這樣你可以通過在視圖中的用戶配置文件的循環:

@model MyViewModel 
@foreach (var profile in Model.UserProfiles) 
{ 
    <div>@profile.SomePropertyOfTheUserProfileClassThatYouWantToDisplayHere</div> 
} 
+0

非常感謝你的回答。請您詳細說明我將如何在視圖中接受和使用它>?我必須使用@model Ienumerable <> MyViewModel?你可以添加一個視圖的代碼微調,你只需爲userProfiles的每個語句執行一次。 – Zapnologica

+0

當然,我已經用一個例子更新了我的答案。 –