Asp.net核心剃须刀页面加载部分页面 [英] Asp.net core razor pages load partial page

查看:352
本文介绍了Asp.net核心剃须刀页面加载部分页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net core 2的新功能Razor页面.在_layout.cshtml中.

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>

_layoutHeader.cshtml是后面带有代码的页面.

the _layoutHeader.cshtml is page with code behind.

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

和@RenderBody将使用pagemodel加载index.cshtml.

and @RenderBody will load index.cshtml with pagemodel.

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

运行项目.引发错误:

InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'Yiko.Ent.WebRazorPages.Pages.Home.IndexModel',但是此ViewDataDictionary实例需要一个类型为'Yiko.Ent.WebRazorPages.Pages._LayoutHeaderModel'的模型项. . Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象值)

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)

推荐答案

您可以重定向到页面,也可以将核心视图代码分为一部分并从两者中调用.

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.

这篇关于Asp.net核心剃须刀页面加载部分页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆