【问题标题】:Two partial views with data from different models but displayed on a same View具有来自不同模型的数据但显示在同一个视图上的两个局部视图
【发布时间】:2017-10-27 21:20:12
【问题描述】:

我有一个类似于以下的表/模型结构,其中父表PT 有两个子表 C1 和 C2,除了指向 PK 列的 FK 列外,它们都有不同的列。我需要在每个子表的两个 HTML 表中显示带有数据的View。我怎样才能做到这一点?问题在于如何从相同的操作方法传递两个模型(每个部分视图一个)。每个部分视图都有一个要显示的记录。注意:我可以在两个不同的视图中显示每个局部视图,每个视图都有自己的模型。但是如何实现以上功能呢?

父表 PT 列:PK_col, col1, col2

子表 C1 列:FK_col, col3, col4

子表 C2 列:FK_col、col5、col6

查看

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

【问题讨论】:

  • 主视图中的模型应该是PT,然后是@Html.Partial("PartialVW_1", Model.C1)@Html.Partial("PartialVW_2", Model.C2)
  • @StephenMuecke 所以Model.C1会因为FK关系自动选择子属性对吗?
  • 是的,如果你在GET方法中正确加载了PT
  • @StephenMuecke 看来我可能没有正确加载PT,因为Model.C1 始终为空,即使在 Db 中它确实有数据。所以,我为此问题创建了another post

标签: asp.net-mvc asp.net-core asp.net-core-1.1


【解决方案1】:

CREDIT to:我通过他的评论abovethis 来自@Tseng 的回复通过@StephenMuecke 的帮助解决了这个问题。

首先,在控制器中我需要加载父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)

【讨论】:

    【解决方案2】:

    创建具有两个子模型的父模型。请参阅下面的代码,希望对您有所帮助。

    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>
    

    【讨论】:

      【解决方案3】:

      你好,其实很简单,就是

      如何从同一个动作方法传递两个模型(每个部分视图一个)?

      您可以使用以下示例将模型传递到局部视图。这可以是页面的视图模型,或者它的一部分,或者一个自定义对象。

      示例:

      @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的概念。

      视图模型的有用链接:

      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

      希望以上对解决问题有帮助,请告诉我您的想法或反馈

      谢谢

      卡提克

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-21
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 2016-02-12
        • 1970-01-01
        相关资源
        最近更新 更多