传递到ViewDataDictionary中的模型项的类型为'MainModel',但是此ViewDataDictionary实例需要类型为'testModel'的模型项]]

问题描述 投票:0回答:1

任何帮助将不胜感激!我保留页面几乎为空,以首先解决此问题。在布局页面中尝试了不同的方式来调用部分视图。使用asp.net core 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>

我有一个空的“主页”页面

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
*@

我的主页使用布局页面。enter image description here

任何帮助将不胜感激!我保留页面几乎为空,以首先解决此问题。在布局页面中尝试了不同的方式来调用部分视图。使用asp.net core 2.1我的局部视图肯定应该...

asp.net-core razor layout partial-views razor-pages
1个回答
0
投票

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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.