【问题标题】:Share a model between view components in dotnet core在 dotnet core 中的视图组件之间共享模型
【发布时间】:2016-07-21 02:37:39
【问题描述】:

我有一个包含多个视图组件的布局,它们都使用相同的视图模型和控制器

那么我怎样才能在它们之间共享同一个视图模型实例呢?

这是我的布局:

<!DOCTYPE html>
<html lang="es">
<head prefix="og: http://ogp.me/ns#">
    @await Component.InvokeAsync("Metadata", Share-Model)
</head>
<body>
    @await Component.InvokeAsync("Header", Share-Model)
    <article id="main-content-article">
        @await Component.InvokeAsync("Cover", Share-Model)
    </article>
    <section id="rest-fold-content">
        @RenderBody()
        <footer id="main-footer">
            @await Component.InvokeAsync("Footer")
        </footer>
    </section>

</body>
</html>

【问题讨论】:

    标签: c# razor asp.net-core asp.net-core-mvc


    【解决方案1】:

    你几乎明白了:

    // inside View Component
    public async Task<IViewComponentResult> InvokeAsync(SharedModel sharedmodel) { ... }
    

    在 *.cshtml 内:

    @model MyApp.Models.SharedModel
    ...
        <article id="main-content-article">
        @await Component.InvokeAsync("Cover", new { sharedmodel = model } )
    </article>
    

    请注意匿名类中的属性与InvokeAsync方法中的参数同名。不过,这一切都清楚地记录在documentation 中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多