Cuiqs
Cuiqs

Reputation: 881

Asp.net core razor pages load partial page

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)

Upvotes: 2

Views: 2245

Answers (1)

RickAndMSFT
RickAndMSFT

Reputation: 22810

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.

Upvotes: 1

Related Questions