在 PageModel 视图 Asp.net 核心应用程序中呈现部分视图时出错

Error occur while rendering Partial View in PageModel View Asp.net Core application

我在共享文件夹中创建了简单的局部视图。 但是当我使用部分标签在 index.cshtml 中渲染它时,它给出了一个错误。

Error: InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'CoreWeb.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'CoreWeb.Pages.Shared._PartialTestModel'.

我使用 <partial name="_PartialTest"> 标签进行渲染

局部视图位置

部分查看内容

在 index.cshtml

中呈现

Startup.cs 与 PartialView

相关的文件更改

错误

<partial> 声明中,您需要传递一个适当的模型,在本例中为 _PartialTestModel:

的一个实例
<partial name="_PartialTest" model="@new _PartialTestModel()" />

参见https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-3.1