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

weixin_38067813 2018-02-14 01:26:48
在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页面,是否可行,如果可行应该用什么方法,具体如何实现?微软官方推荐的方法机制是什么?有相关的资料有吗?
...全文
544 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38109674 2018-02-16
  • 打赏
  • 举报
回复
非常感谢您的帮助,并恭祝新年快乐,犬(全)年合家欢乐,幸福健康。
weixin_38097808 2018-02-16
  • 打赏
  • 举报
回复
你好,
是这样的, 微软的PG说只有这两种方法, 我贴的链接如果切换到英文版,有FAQ。
具体如下


weixin_38088281 2018-02-14
  • 打赏
  • 举报
回复
再次感谢您的帮助,今天下午我测试了一下午的iframe,要想正常启用并不容易,操作步骤比较多,并不能简洁的调用,请问还有什么其他的方法吗?
而且iframe对移动端支持不友好,基本没法正常使用啊。
weixin_38073436 2018-02-14
  • 打赏
  • 举报
回复
感谢您的回复,我想请问下  iframe 不是已经不推荐用了吗,另外,如果用mvc,Razor页面的优势不就没了吗,还有没有其他完善的解决方案。
个人感觉如果能直接调用Razor页面嵌入到index.cshtml中去,那要比局部视图、视图组件等简单的多。

weixin_38071610 2018-02-14
  • 打赏
  • 举报
回复
你好,
据我所知,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

474

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧