2
votes

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
Pass the model @Html.PartialAsync("_LayoutHeader", {your model}) Svek
And this is why Razor Pages suck, and why Web Forms should have been left in the dustbin of history, instead of resurrected as Razor Pages.Chris Pratt

1 Answers

1
votes

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.