2014-05-09 68 views
0

相對較新的MVC,無法弄清楚如何將數據從多個模型傳遞到單個視圖。我知道我需要使用視圖模型,但我無法弄清楚在控制器中要做什麼。將多個模型傳遞到視圖MVC

型號:

public class Child 
{ 
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)] 
public int ChartNumber { get; set; } 
[Display(Name = "First Name")] 
public string FirstName { get; set; } 
[Display(Name = "Last Name")] 
public string LastName { get; set; } 
public string City { get; set; } 
public string State { get; set; } 
[Display(Name = "Zip Code")] 
public int ZipCode { get; set; } 
public string Ethnicity { get; set; } 
public string Referral { get; set; } 
[Display(Name = "Recommended Re-evaluation")] 
public bool ReEval { get; set; } 
[Display(Name = "Hearing Aid Candidate")] 
public bool HaCandidate { get; set; } 
[Display(Name = "Fit With Hearing Aid")] 
public bool FitHa { get; set; } 

public virtual List<ChildEval> ChildEvals { get; set; } 
} 

public class ChildEval 
{ 
[Key] 
public int ChildEvalId { get; set; } 
public DateTime Date { get; set; } 
     public int PtaRight { get; set; } 
     public int PtaLeft { get; set; } 
     public int UnaidedSiiRight { get; set; } 
     public int UnaidedSiiLeft { get; set; } 
     public int AidedSiiRight { get; set; } 
     public int AidedSiiLeft { get; set; } 
     public int ChartNumber { get; set; } 

     public virtual Child Child { get; set; } 
    } 
} 

的DbContext

公共類UnitedContext:的DbContext {

public UnitedContext() : base("name=UnitedContext") 
    { 
    } 

    public System.Data.Entity.DbSet<United.Models.Child> Children { get; set; } 
    public System.Data.Entity.DbSet<United.Models.ChildEval> ChildEvals { get; set; } 

} 

視圖模型:

public class ChildViewModel 
    { 
     public class Child 
     { 
      public string FirstName { get; set; } 
     } 
     public class ChildEval 
     { 
      public int PtaRight { get; set; } 
     } 
    } 

ViewModelController? :

public class ViewModelController : Controller 
    { 
     // 
     // GET: /ViewModel/ 
     public ActionResult Index() 
     { 

      return View(); 
     } 
    } 
} 

我卡在如何創建視圖模型和如何實際的數據獲取到視圖控制器。任何幫助將不勝感激!

回答

1

沒有一個名爲ViewModelController的控制器。控制器是數據庫和視圖之間的處理程序。因此控制器的名稱應告訴程序員控制器正在控制什麼樣的數據。默認情況下,它也表示您在Web應用程序中的子網站(ViewModelController將產生http://mysite/ViewModel/)。

由於您正在處理孩子,我現在稱它爲ChildrenController。

一般來說,一個好主意是將你的2個模型包裝到一個視圖模型中並使用它。在這種情況下,Model!= ViewModel,model是實體的數據庫模型,而ViewModel是傳遞給視圖的視圖模型。

public class Child 
{ 
    public int ID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 
public class SomeOtherModel 
{ 
    public int ID { get; set; } 
    public int SomeInteger { get; set; } 
    public int SomeOtherInteger { get; set; } 
} 

public class ChildViewModel 
{ 
    public Child Child { get; set; } 
    public SomeOtherModel SomeOtherModel { get; set; } 

    public ChildViewModel(Child child, SomeOtherModel s) 
    { 
     Child = child; 
     SomeOtherModel = s; 
    } 
} 

在你的控制器

public class ChildrenController : Controller 
{ 
    // 
    // GET: /Children/ 
    public ActionResult Index() 
    { 
     Child child = /*Fetch the child from database*/; 
     SomeOtherModel someOther = = /*Fetch something else from database*/; 

     ChildViewModel model = new ChildViewModel(child,someOther); 

     return View(model); 
    } 
} 

查看:

@model ChildViewModel 

@Html.EditorFor(model => model.Child.FirstName) 
@Html.EditorFor(model => model.Child.LastName)