mvc核心中的局部视图

时间:2017-12-26 08:29:47

标签: asp.net-mvc linq

我想查看视图模型的表列是

public class ColumnTotal {
    public int ID { get; set; }
    public decimal ColumnOne { get; set; }
    public decimal ColumnTwo { get; set; }
    public decimal ColumnThree { get; set; }
}

我的columntotal方法的家庭控制器看起来像这样

 public IActionResult ColumnTotal()
    {
        var query = _context.REHPData.Include(r => r.BudgetYear).GroupBy(r => r.BudgetYearlyAllocation.ID).Select(s => new ColumnTotal
        {
            ID = s.Key,
            ColumnOne = s.Sum(x => x.BudgetYearlyAllocation.AdminThaTeen),
            ColumnTwo = s.Sum(x => x.BudgetYearlyAllocation.AdminThaTwo),
            ColumnThree = s.Sum(x => x.BudgetYearlyAllocation.AdminThaFour)

        }).ToList();
        return View(query);

    }

我的部分视图_test.cshtml看起来像这样

  @model RenewableEnergyProjcet.Models.CalculationViewModels.ColumnTotal
    <tr>
        <td> @Html.DisplayFor(m=>m.ColumnOne)</td>
        <td> @Html.DisplayFor(m=>m.ColumnTwo)</td>
        <td> @Html.DisplayFor(m=>m.ColumnThree)</td>
    </tr>

我的report.chtml看起来像这样

@{
    ViewData["Title"] = "Budget Result Table ";
}

<h2>Budgets Result Table  </h2>

<table class="table table-bordered table-condensed table-responsive table-hover">
    <tr>
       <td>Some Columns -----</td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            some data----
        </tr>
    }

        <tr>
            @await Html.PartialAsync("_test"); //how to call this partial _test.chtml
        </tr>

</table>

请如何从方法中调用部分_test.chtml。

1 个答案:

答案 0 :(得分:0)

在Action中,填充report.chtml也会调用您在ColumnTotal()操作中使用的查询并将其发送到视图,并在视图中使用RenderPartial()来调用视图。

public IActionResult report()
{
    ViewData["ColumnTotal"] = _context.REHPData.Include(r => r.BudgetYear).GroupBy(r => r.BudgetYearlyAllocation.ID).Select(s => new ColumnTotal
    {
        ID = s.Key,
        ColumnOne = s.Sum(x => x.BudgetYearlyAllocation.AdminThaTeen),
        ColumnTwo = s.Sum(x => x.BudgetYearlyAllocation.AdminThaTwo),
        ColumnThree = s.Sum(x => x.BudgetYearlyAllocation.AdminThaFour)

    }).ToList();
    return View("report");

}

在视图中执行类似的操作。

@Html.RenderPartial("_test",ViewData["ColumnTotal"])