【问题标题】:partial view in mvc coremvc核心中的部分视图
【发布时间】:2017-12-26 08:29:46
【问题描述】:

我想对视图模型的表列求和

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。

【问题讨论】:

  • 你写过返回局部视图的动作吗?
  • 我不明白,请帮帮我
  • 我在核心方面没有太多经验,但如果我要像在 mvc 中那样做,那么您将需要像这样的控制器操作 public PartialViewResult Test() { return PartialView("_test",new YourViewModel()); }
  • 并在主视图中,而不是使用@await 使用:-- @Html.RenderPartial("_test",YourViewModelObject)
  • @paingsoethu2015,你确定你需要这个局部视图来显示计算的页脚吗?可以尝试组合两个请求的变体吗?首先是返回主表的查询,其次是返回页脚的查询,并使用ConcatUnion 将它们组合起来。如果是这样,则不需要部分视图并将第二个查询发送到服务器。

标签: asp.net-mvc linq


【解决方案1】:

在填充 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"])

【讨论】:

猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2021-08-14
  • 2021-02-17
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多