abp - 如何将包含数据的模型从数据库传递到 ABP.IO 布局 Hook ?

标签 abp

尝试使用 ABP.io 框架 3.1 设置 Multi-Tenancy 站点。

我正在尝试在页面 html 头部设置 <meta 关键字(以及其他标签)。我正在尝试从当前租户的数据库字段中获取值,因此元关键字将特定于租户。 我尝试遵循此处提供的示例:https://docs.abp.io/en/abp/latest/UI/AspNetCore/Customization-User-Interface#layout-hooks他们将谷歌分析脚本代码注入(inject)到 head 标签中。

这很好,因为它是静态文本,但是当我尝试使用模型加载部分页面时,它会抛出一个错误,提示需要与传入的模型不同的模型。

到目前为止我已经有了通知 View 组件

Public class MetaKeywordViewComponent : AbpViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync() {
        return View("/Pages/Shared/Components/Head/MetaKeyword.cshtml"); //, meta);
    }
}

和cshtml页面

@using MyCompany.MyProduct.Web.Pages.Shared.Components.Head
@model  MetaKeywordModel 

@if (Model.SiteData.Keywords.Length > 0)
{
    <meta content="@Model.SiteData.Keywords" name="keywords" />
}

cshtml.cs 文件为

 public class MetaKeywordModel : MyProductPageModel
    {
        private readonly ITenantSiteDataAppService _tenantSiteDataAppService;

        public TenantSiteDataDto SiteData { get; private set; }

        public MetaKeywordModel(ITenantSiteDataAppService tenantSiteDataAppService)
        {
            _tenantSiteDataAppService = tenantSiteDataAppService;
        }

        public virtual async Task<ActionResult> OnGetAsync()
        {
            if (CurrentTenant != null)
            {
                SiteData = await _tenantSiteDataAppService.GetSiteDataAsync();
            }

            return Page();
        }
    }

但是当我运行该程序时,出现以下错误。

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook.LayoutHookViewModel', but this ViewDataDictionary instance requires a model item of type 'MyCompany.MyProduct.TenantData.Dtos.TenantSiteDataDto'.
 

如果我无法使用我的模型,如何将数据从数据库传递到要呈现的页面?

任何帮助提示或技巧将不胜感激。

问候 马蒂

最佳答案

ViewComponent 与 razor 页面不同。

参见https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.1#view-components

您应该直接将服务注入(inject) View 组件类中。像:

public class MetaKeywordViewComponent : AbpViewComponent
{
    private readonly ITenantSiteDataAppService _tenantSiteDataAppService;

    public MetaKeywordViewComponent(ITenantSiteDataAppService tenantSiteDataAppService)
    {
        _tenantSiteDataAppService = tenantSiteDataAppService;
    }

    public async Task<IViewComponentResult> InvokeAsync()
    {
        return View("/Pages/Shared/Components/Head/MetaKeyword.cshtml",
            await _tenantSiteDataAppService.GetSiteDataAsync());
    }
}

另外,您可以引用https://github.com/abpframework/abp/blob/42f37c5ff01ad853a5425d15539d4222cd0dab69/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Themes/Basic/Components/PageAlerts/PageAlertsViewComponent.cs

关于abp - 如何将包含数据的模型从数据库传递到 ABP.IO 布局 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63821517/

相关文章:

azure - ABP 框架 6.0.0 在 Azure 上部署时失败

asp.net-core - 没有 ORM 的 ABP

c# - 我可以在并行线程中更新一个实体吗?

aspnetboilerplate - 正确实现忘记密码 AspNetBoilerPlate

asp.net-mvc - ABP Suite 似乎正在生成无法编译的代码

ubuntu - 如何将 ABP 框架应用程序部署到 Ubuntu 服务器?

abp - ABP MVC项目如何从Lepton Theme切换到Basic Theme

abp - 如何在 abp 框架中播种 AppUser

c# - EfCore 与 DDD 中的多对多关系

abp - 关于使用 DomainService