jac*_*per 2 c# razor asp.net-core-mvc asp.net-core
我有一个包含多个视图组件的布局,它们都使用相同的视图模型和控制器
那么我该如何在它们之间共享相同的视图模型实例呢?
这是我的布局:
<!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>
Run Code Online (Sandbox Code Playgroud)
你几乎明白了:
// inside View Component
public async Task<IViewComponentResult> InvokeAsync(SharedModel sharedmodel) { ... }
Run Code Online (Sandbox Code Playgroud)
*.cshtml 里面:
@model MyApp.Models.SharedModel
...
<article id="main-content-article">
@await Component.InvokeAsync("Cover", new { sharedmodel = model } )
</article>
Run Code Online (Sandbox Code Playgroud)
请注意,匿名类中的属性与方法中的参数具有相同的名称InvokeAsync。不过,这一切都清楚地记录在文档中。
| 归档时间: |
|
| 查看次数: |
4102 次 |
| 最近记录: |