用兩個孩子模型創建父模型。看到下面的代碼,我希望這可以幫助你。
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>
主視圖中的模型應該是「PT」,然後是「@ Html.Partial(」PartialVW_1 「,Model.C1)'和'@ Html.Partial(」PartialVW_2「,Model.C2)' –
@StephenMuecke那麼'Model.C1'會自動選擇子屬性,因爲FK關係正確嗎? – nam
是的,如果你正確地在GET方法中加載了'PT' –