我有一个带有布局页面的 asp.net 核心项目:
_Layout.cshtml
这是默认的 asp.net core _Layout 页面。不涉及模型。
在Areas
文件夹中,我有几个文件:
Vendor > Pages > _ViewStart.cshtml
Vendor > Pages > _Menu.cshtml
Vendor > Pages > Index.cshtml
这些文件如下所示:
_ViewStart
@{
Layout = "/Views/Shared/_Layout.cshtml";
}
@{ await Html.RenderPartialAsync("_Menu"); }
_菜单
@page
@model MyProject.Areas.Vendor.Pages.MenuModel
@{
foreach(var i in Model.MenuItems)
{
@Html.ActionLink(i.DisplayText,i.Url)
}
}
指数
@page
@model MyProject.Areas.Vendor.Pages.IndexModel
@{
}
<h1>Hello, world!</h1>
现在我想访问Vendor
索引 Razor 页面,但我希望它也_Menu
首先呈现选项,因此_ViewStart
with the_Layout
和_Menu
- 基本上我访问的每个供应商页面都应该_Menu
首先呈现。
但是当我访问索引时,我得到了这个错误:
The model item passed into the ViewDataDictionary is of type 'MyProject.Areas.Vendor.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'MyProject.Areas.Vendor.Pages.MenuModel'
怎么会这样?我没有向Index
页面传递任何内容?当我试图指定null
作为参数时,RenderPartialAsync()
我就进入null reference exception
了我的 _Menu 页面。
我怎样才能达到我所需要的?
它应该呈现 _Layout > _ViewStart > _Menu > AnyOtherVendorPage