【问题标题】:how to render a partial view from controller in asp.net core如何在asp.net核心中从控制器呈现部分视图
【发布时间】:2020-02-11 06:55:58
【问题描述】:

我在控制器中有一个动作来为Partial view 加载数据,如下所示:

    public async Task<IActionResult> ParentTutorCountByLocation()
    {
        var result = await this.dashboardService.GetParentTutorCountPerLocation();
        return this.PartialView("Dashboard/ParentTutorCountByLocation", result);
    }

我必须在另一个视图上调用它:

尝试过,renderPartialAsync,部分

<div class="row">
    <partial name="ParentTutorCountByLocation" />
</div>

它是加载部分但没有数据不知道我该怎么做。 (不能使用视图组件和 ajax)

【问题讨论】:

  • 为什么不明确传递model,例如&lt;partial name="ParentTutorCountByLocation" model="@TheModelForThePartialView" /&gt;?
  • “不能使用视图组件”为什么?这正是视图组件的用途。部分不能在类似控制器的操作中运行自己的逻辑。

标签: asp.net-core partial-views


【解决方案1】:

您需要使用 Html.Action 之类的东西从控制器渲染部分视图。不幸的是,ASP.NET Core 不支持这个开箱即用。你可以找到Html.Action实现here

一旦你实现了,你就可以做这样的事情。

@Html.Action("YourPartialView")

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 1970-01-01
    • 2018-10-09
    • 2011-05-19
    • 1970-01-01
    • 2018-03-09
    • 2018-12-27
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多