2017-05-26 87 views
0

我有一個類似於以下的表/模型結構,父表PT有兩個子表C1和C2,兩個子表除了FK列指針到PK欄。我需要顯示一個View與兩個HTML表中的每個子表中的數據。我怎樣才能做到這一點?問題在於如何從相同的操作方法傳遞兩個模型(每個部分視圖一個模型)。每個局部視圖都有一個要顯示的記錄。注意:我可以在兩個不同的視圖中顯示每個局部視圖,每個視圖都有自己的模型。但如何實現上述?來自不同模型但顯示在同一視圖中的數據的兩個部分視圖

父表PT列:PK_col,COL1,COL2

子表C1列:FK_col,COL3,COL4

子表C2列:FK_col,COL5,COL6

查看

... 
@Html.Partial("PartialVW_1") 
... 
@Html.Partial("PartialVW_2") 
.... 
+2

主視圖中的模型應該是「PT」,然後是「@ Html.Partial(」PartialVW_1 「,Model.C1)'和'@ Html.Partial(」PartialVW_2「,Model.C2)' –

+0

@StephenMuecke那麼'Model.C1'會自動選擇子屬性,因爲FK關係正確嗎? – nam

+0

是的,如果你正確地在GET方法中加載了'PT' –

回答

0

歸功於:我通過解決了這個問題@StephenMuecke的幫助通過他的評論above和通過this響應@Tseng

首先,在控制器我需要加載的父PT和兒童C1C2如下:

控制器

.... 
.... 
PT pt = _context.PT 
    .Include(c => c.C1) // Add these two lines to tell EF Core to load C1 and C2 as well 
    .Include(c => c.C2) // 
    .Where(c=> c.PTId== selectedId).SingleOrDefault(); 
.... 
.... 

然後,在主視圖中的模式應該是PT和然後@Html.Partial("PartialVW_1", Model.C1)@Html.Partial("PartialVW_2", Model.C2)

0

嗨南它其實很簡單,那就是

如何從相同的動作方法傳遞兩個模型(每個部分視圖一個)?

通過使用下面的示例,可以將模型傳遞到局部視圖。這可以是頁面的視圖模型或其某個部分,也可以是自定義對象。

實施例:

@Html.Partial("PartialName", viewModel) 

在你的情況你的意見應該是:

主視圖:

@model PT 
{ 

@Html.Partial("PartialVW_1",Model.C1) 
@Html.Partial("PartialVW_1",Model.C2) 
} 

局部視圖1:

@model c1 
{ 
} 

局部圖2:

@model c2 
{ 
} 

注意:如果你必須通過模型正確的父子關係,那麼它會給根據PK和FK約束的數據。

附加信息: [僅供參考]

如果你想在不同的多個對象或複雜對象傳遞給視圖,您還可以使用視圖模型的MVC中的概念。

有用的鏈接視圖模型:

http://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-3

希望上面是有幫助解決問題,請讓我知道你的想法或反饋

感謝

Karthik

0

用兩個孩子模型創建父模型。看到下面的代碼,我希望這可以幫助你。

public class ParentModel 
{ 
    public Int64 PK { get; set; } 
    public string col1 { get; set; } 
    public Int64 col2 { get; set; } 
    public string col3 { get; set; } 
    public List<ChildTable1> ChildTable1 { get; set; } 
    public List<ChildTable2> ChildTable2 { get; set; } 
} 
public class ChildTable1 
{ 
    public Int64 PK { get; set; } 
    public string col1 { get; set; } 
    public Int64 col2 { get; set; } 
    public string col3 { get; set; } 


} 
public class ChildTable2 
{ 
    public Int64 PK { get; set; } 
    public string col1 { get; set; } 
    public Int64 col2 { get; set; } 
    public string col3 { get; set; } 
} 

父視圖

@model ParentModel 


@using (Html.BeginForm("Action", "Controller")) 
{ 
    @{Html.RenderPartial("~/Views/Partial1.cshtml", new { Model = Model });} 
    @{Html.RenderPartial("~/Views/Partial2.cshtml", new { Model = Model });} 
} 

在局部視圖1

@model ParentModel 
<table> 
    <tbody> 
     <tr> 
      <th>col1</th> 
      <th>col2</th> 
      <th>col3</th> 
     </tr> 
     @foreach (var item in Model.ChildTable1) 
     { 
      <tr> 

       <td>@item.col1</td> 
       <td>@item.col2</td> 
       <td>@item.col3</td> 
      </tr> 
     } 
    </tbody> 


</table> 

在局部視圖2

@model ParentModel 
<table> 
    <tbody> 
     <tr> 
      <th>col1</th> 
      <th>col2</th> 
      <th>col3</th> 
     </tr> 
     @foreach (var item in Model.ChildTable2) 
     { 
      <tr> 

       <td>@item.col1</td> 
       <td>@item.col2</td> 
       <td>@item.col3</td> 
      </tr> 
     } 
    </tbody> 


</table> 
相關問題