2013-08-01 60 views
1

我正在創建一個ASP MVC 3中的數據網格,其中我在一個視圖中有兩個表。每個表格都在使用自己的模型。因此,我必須將兩個模型調用到一個View中,這看起來並不像我希望的那樣簡單。ASP MVC 3在一個視圖中的兩個模型

我非常新的MVC和我一直在尋找通過堆棧,發現此鏈接: Two models in one view in ASP MVC 3

這似乎是我想要去的方向......我想。

這裏是我的第一個型號代碼:

[Table] 
public class Model1 
{ 
    [Column(IsPrimaryKey = true, IsDbGenerated = true)] 
    public int Column1 { get; set; } 
    [Column] 
    public string Column2 { get; set; } 
    [Column] 
    public string Column3 { get; set; } 
} 

這裏是我的第二個模型的代碼:

[Table] 
public class Model2 
{ 
    [Column(IsPrimaryKey = true, IsDbGenerated = true)] 
    public int Column1 { get; set; } 
    [Column] 
    public int Column2 { get; set; } 
    [Column] 
    public string Column3 { get; set; } 
    [Column] 
    public string Column4 { get; set; } 
} 

這裏是父視圖模型的代碼,其他堆棧論壇建議:

public class ParentModelView 
{ 
    public Model1 Model1 { get; set; } 
    public Model2 Model2 { get; set; } 
} 

我能夠讓每個人單獨工作,所以我知道它不是任何ot她的問題。另一個Stack論壇似乎對我的理解有些模糊。我覺得這不僅僅是增加另一個使用其中的每個模型的父模型(我正在從中獲得什麼)。

您可能會覺得有用的一些其他信息是,每個模型都在它自己的文件中。此外,這是我的錯誤:

The model item passed into the dictionary is of type 
'System.Data.Linq.Table 1[Model1]', but this dictionary requires a model 
item of type 'System.Collections.Generic.IEnumerable 1[ParentModelView]'. 

Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information 
about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed 
into the dictionary is of type 'System.Data.Linq.Table 1[Model1]', but this 
dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable 1[ParentModelView]'. 

Source Error: 

An unhandled exception was generated during the execution of the current 
web request. Information regarding the origin and location of the exception 
can be identified using the exception stack trace below. 

---編輯1 ---

這裏是我查看裏面的代碼:

@model IEnumerable<ParentModelView> 
@{ 
    WebGrid gridModel1 = new WebGrid(Model); 
    WebGrid gridModel2 = new WebGrid(Model); 
} 
<div class="Table"> 
    <h2>Model1</h2> 
    @gridModel1.GetHtml(columns: new[] { 
     gridModel1.Column("Column1"), 
     gridModel1.Column("Column2"), 
     gridModel1.Column("Column3") 
    }) 
</div> 

<div class="Table" id="rightTable"> 
    <h2>Model2</h2> 
    @*@gridModel2.GetHtml(columns: new[] { 
     gridModel2.Column("Column1"), 
     gridModel2.Column("Column2"), 
     gridModel2.Column("Column3"), 
     gridModel1.Column("Column4") 
    })*@ 
</div> 

EDIT 2

正如你們大部分人所要求的,這裏是我的控制器代碼。我知道這是不對的,因爲我不太清楚如何將兩個模型之間的信息傳遞給相同的視圖。如果有人能夠爲此提供幫助,將會非常感激。

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 

     Model1Repo repoModel1 = new Model1Repo(); 
     var Model1RepoSQL = repoModel1.All(); 

     Model2Repo repoModel2 = new Model2Repo(); 
     var Model2RepoSQL = repoModel2.All(); 

     return View(Model1RepoSQL); 
    } 
} 

的任何其他信息,將不勝感激。謝謝!

+0

錯誤表明視圖期望子模型,而不是父模型。你能發佈你的觀點的相關部分嗎? – McGarnagle

+0

@McGarnagle - 請在我的職位代碼編輯的我查看裏面。 – scapegoat17

+1

你能後的操作方法,以及這樣我們就可以看到你傳遞什麼樣的看法? – asymptoticFault

回答

3

我想你想要的是更多的東西是這樣的:

public class ParentModelView 
{ 
    public IEnumerable<Model1> Model1 { get; set; } 
    public IEnumerable<Model2> Model2 { get; set; } 
} 

而且在你看來

@model ParentModelView 

@{ 
    WebGrid gridModel1 = new WebGrid(Model.Model1); 
    WebGrid gridModel2 = new WebGrid(Model.Model2); 
} 

編輯;

顯然,你是不是正確填充父模型。我以爲你會明白如何做到這一點。

public ActionResult MyAction() { 
    var model1 = // get rows of model1 
    var model2 = // get rows of model2 

    return View("myview", new ParentModelView { Model1 = model1, Model2 = model2 }) ; 
} 
+0

我想你的解決方案,但它似乎仍然有同樣的錯誤。 – scapegoat17

+0

@jpriff - 這意味着你沒有將正確的模型數據傳遞給視圖。您需要向我們顯示您的控制器代碼。 –

+0

謝謝!你最後的編輯做了訣竅。我不確定如何填充父模型。我總共有MVC的n00b ... – scapegoat17

0

在這種情況下,您始終可以使用ViewModel。例如創建一個視圖模型

public class ViewModel{ 

    public int Table1Column1 { get; set; } 
    public string Table1Column2 { get; set; } 
    public string Table1Column3 { get; set; } 


    public int Table2Column1 { get; set; } 
    public int Table2Column2 { get; set; } 
    public string Table2Column3 { get; set; } 
    public string Table2Column4 { get; set; } 

} 

從無論是在業務層或數據訪問層模型獲取數據到ViewModel

P.S:McGarnagle說的是真實的,但反過來,你期待ParentModelView,你正在發送兒童模型進入視圖。如果您可以發佈控制器和視圖的部分代碼,這將會很有幫助。

相關問題