Asp.net core razor pages load partial page

試著忘記壹切 提交于 2019-12-21 21:42:27

问题


I use asp.net core 2 new feature Razor pages. in the _layout.cshtml.

 <body>
<div>
   @await Html.PartialAsync("_LayoutHeader") 
    <div class="row">
        <div>
            <div class="row">
                @RenderBody()
            </div>
        </div>
    </div>

the _layoutHeader.cshtml is page with code behind.

@page
@using Microsoft.AspNetCore.Identity
@model Yiko.Ent.WebRazorPages.Pages._LayoutHeaderModel

and @RenderBody will load index.cshtml with pagemodel.

@page
@model Yiko.Ent.WebRazorPages.Pages.Home.IndexModel 
@{
    ViewData["Title"] = "Home";
}

run the project. throw a error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Yiko.Ent.WebRazorPages.Pages.Home.IndexModel', but this ViewDataDictionary instance requires a model item of type 'Yiko.Ent.WebRazorPages.Pages._LayoutHeaderModel'. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)


回答1:


You can redirect to the page, or you can make the core view code into a partial and call it from both.

Pages are not a replacement for partials or View Components.



来源:https://stackoverflow.com/questions/46906846/asp-net-core-razor-pages-load-partial-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!