具有来自不同模型但显示在同一视图上的数据的两个部分视图
Two partial views with data from different models but displayed on a same View
我有一个 table/model 结构类似于下面的结构,其中父 table PT
有两个子 tables C1 和 C2,它们都有不同的列,除了FK 列指向 PK 列。我需要为每个子 table 显示两个 HTML table 中的数据的 View
。我怎样才能做到这一点?问题在于如何从同一操作方法传递两个模型(每个部分视图一个)。每个局部视图都显示一条记录。注意:我可以在两个不同的视图中显示每个局部视图,每个视图都有自己的模型。但是如何实现以上呢?
父级 table PT 列:PK_col、col1、col2
子 table C1 列:FK_col、col3、col4
子 table C2 列:FK_col、col5、col6
查看:
...
@Html.Partial("PartialVW_1")
...
@Html.Partial("PartialVW_2")
....
你好,其实很简单,就是
如何从同一操作方法传递两个模型(每个部分视图一个)?
您可以使用以下示例将模型传递到分部视图中。这可以是页面的视图模型,或者它的一部分,或者一个自定义对象。
示例:
@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中ViewModel的概念。
对视图模型有用link:
http://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc
希望以上对解决问题有所帮助,请告诉我您的想法或反馈
谢谢
卡尔提克
创建具有两个子模型的父模型。请参阅下面的代码,希望对您有所帮助。
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>
CREDIT 转到:我通过 @Tseng
的评论 and through 回复在 @StephenMuecke
的帮助下解决了问题。
首先,我需要在控制器中加载父项 PT
和子项 C1
、C2
,如下所示:
控制器:
....
....
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)
我有一个 table/model 结构类似于下面的结构,其中父 table PT
有两个子 tables C1 和 C2,它们都有不同的列,除了FK 列指向 PK 列。我需要为每个子 table 显示两个 HTML table 中的数据的 View
。我怎样才能做到这一点?问题在于如何从同一操作方法传递两个模型(每个部分视图一个)。每个局部视图都显示一条记录。注意:我可以在两个不同的视图中显示每个局部视图,每个视图都有自己的模型。但是如何实现以上呢?
父级 table PT 列:PK_col、col1、col2
子 table C1 列:FK_col、col3、col4
子 table C2 列:FK_col、col5、col6
查看:
...
@Html.Partial("PartialVW_1")
...
@Html.Partial("PartialVW_2")
....
你好,其实很简单,就是
如何从同一操作方法传递两个模型(每个部分视图一个)?
您可以使用以下示例将模型传递到分部视图中。这可以是页面的视图模型,或者它的一部分,或者一个自定义对象。
示例:
@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中ViewModel的概念。
对视图模型有用link:
http://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc
希望以上对解决问题有所帮助,请告诉我您的想法或反馈
谢谢
卡尔提克
创建具有两个子模型的父模型。请参阅下面的代码,希望对您有所帮助。
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>
CREDIT 转到:我通过 @Tseng
的评论 @StephenMuecke
的帮助下解决了问题。
首先,我需要在控制器中加载父项 PT
和子项 C1
、C2
,如下所示:
控制器:
....
....
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)