询问者
在asp.net core razor页面系统中,我在cshtml中如何调用其他的Razor页面?

问题
-
在pages文件夹中,我在index.cshtml页面中想要调入其他的带数据模型的Razor页面应如何操作。
代码如下:
<div class="col-md-3"> <h2>测试系列1</h2> @*@await Html.PartialAsync("ceshi")*@ @await Component.InvokeAsync("ceshi2") </div> <div class="col-md-3"> <h2>测试系列2</h2> @*<ul> <li><a href="https://go.microsoft.com/fwlink/?linkid=852130">Working with Razor Pages.</a></li> <li><a href="https://go.microsoft.com/fwlink/?LinkId=699315">Manage User Secrets using Secret Manager.</a></li> <li><a href="https://go.microsoft.com/fwlink/?LinkId=699316">Use logging to log a message.</a></li> <li><a href="https://go.microsoft.com/fwlink/?LinkId=699317">Add packages using NuGet.</a></li> <li><a href="https://go.microsoft.com/fwlink/?LinkId=699319">Target development, staging or production environment.</a></li> </ul>*@ </div>
具体如图:
当我用Html.PartialAsync时,页面提示我“InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'qtysh.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'qtysh.Pages.ceshiModel'.”
当我用@await Component.InvokeAsync("ceshi")时,页面提示我“InvalidOperationException: A view component named 'ceshi2' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.”
现在的问题是:我在index.cshtml中调用ceshi.cshtml页面,是否可行,如果可行应该用什么方法,具体如何实现?微软官方推荐的方法机制是什么?有相关的资料有吗?
- 已编辑 希望不灭 2018年2月14日 1:28
全部回复
-
你好,
据我所知,razor page 和 MVC里面的Partial View 是不一样的。
我们不能使用Html.Partial去调用razor page。
如果你想要在主页面里面调用razor page,请使用iframe。
或者你可以创建MVC view, 然后使用Html.Partial去调用。
具体如何创建MVC view,你可以参照如下图片。
详细的你可以参照下面这篇文章。
https://docs.microsoft.com/zh-cn/aspnet/core/mvc/razor-pages/?tabs=visual-studio#see-also
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
你好,
是这样的, 微软的PG说只有这两种方法, 我贴的链接如果切换到英文版,有FAQ。
具体如下
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.