【发布时间】:2018-04-30 14:16:27
【问题描述】:
我有一个侧边栏视图作为部分视图。我想把它插入主页。 侧边栏从数据库中读取一些图像 url 并将其显示到 html 页面中。 在主页中,我使用此代码插入侧边栏:
@await Html.PartialAsync("Shared/_Sidebar")
侧栏cshtml页面:
@model MyWeb.Pages.Shared._SidebarModel
...
但它会引发错误:
The model item passed into the ViewDataDictionary is of type 'MyWeb.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'MyWeb.Pages.Shared._SidebarModel'
我研究过这个问题,得到的答案是,必须从父页面传递模型,像这样:
@await Html.PartialAsync("Shared/_Sidebar", Model.Urls)
如果是这样,父页面必须处理子页面的业务。所以,如果我不将侧边栏插入到主页面,而是插入到另一个页面,我必须把主页面上的代码去掉,然后在新页面中添加业务代码,以此类推……侧边栏无法处理自己的事业。 在 MVC 中,我们可以使用 RenderAction 插入部分视图及其操作以处理业务逻辑。但我还没有在 RazorPages 中找到这样做的方法。 谁来帮帮我!
【问题讨论】:
标签: razor asp.net-core razor-pages