传入 ViewDataDictionary 的模型项的类型为 'MainModel',但此 ViewDataDictionary 实例需要类型为 'testModel' 的模型项

model item passed into the ViewDataDictionary is of type 'MainModel', but this ViewDataDictionary instance requires a model item of type 'testModel'

任何帮助将不胜感激! 我让页面几乎是空的,以首先解决这个问题。 在布局页面中尝试了不同的方式来调用局部视图。 使用 asp.net 核心 2.1

我的部分视图肯定会比主页加载得更早。现在它是空的,但它将包含一些要加载的数据。

这是我的单次调用部分视图的布局:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div>
        <table>
            <tr>
                @await Html.PartialAsync("~/Pages/PartialPages/test.cshtml");
                @*<partial name="~/Pages/PartialPages/test.cshtml" />*@
            </tr>
        </table>
    </div>

    <div class="content">
    <div style="float: left; margin-left: 1%; width: 100%">
        @RenderBody()
    </div>
    </div>

    @RenderSection("Scripts", required: false)
</body>
</html>

我有空 "Main" 页

public class MainModel : PageModel
    {
        public void OnGet()
        {

        }
    }

@page
@model APWeb.Pages.MainModel
@{
    ViewData["Title"] = "Main";
}

<h2>Main</h2>

这是我的部分视图页面:

public class testModel : PageModel
    {
        public void OnGet()
        {

        }
    }

@page
@model APWeb.Pages.PartialPages.testModel
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@

我的主页使用布局页。

初始化布局时,您需要将默认模型传递给 test.cshtml:

@await Html.PartialAsync("~/Pages/PartialPages/test.cshtml",new testModel());