Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 2.0 RazorPages:模型未通过页面模型部分实例化_Asp.net Core 2.0_Razor Pages - Fatal编程技术网

Asp.net core 2.0 RazorPages:模型未通过页面模型部分实例化

Asp.net core 2.0 RazorPages:模型未通过页面模型部分实例化,asp.net-core-2.0,razor-pages,Asp.net Core 2.0,Razor Pages,我正在测试RazorPages和.Net内核2.1 我刚刚采取了一个新的项目模板,并创建了一个部分。 这些是文件的相关/添加内容 我的问题是 1) 直接问题:在部分:OnGetAsync(或public void OnGet())未被调用。我在模型在线上看到了一个空参考例外 @foreach (var item in Model.ImageBE) { 我曾试图切断DB呼叫,并从contructor处无条件地呼叫OnGet,但没有区别 2) 我找不到一个页面(索引)有Partials模型(下面是

我正在测试RazorPages和.Net内核2.1

我刚刚采取了一个新的项目模板,并创建了一个部分。 这些是文件的相关/添加内容

我的问题是

1) 直接问题:在部分:OnGetAsync(或public void OnGet())未被调用。我在模型在线上看到了一个空参考例外

@foreach (var item in Model.ImageBE) {
我曾试图切断DB呼叫,并从contructor处无条件地呼叫OnGet,但没有区别

2) 我找不到一个页面(索引)有Partials模型(下面是ImageGalleryModel)实例的例子。但这是编译器唯一能接受的。我这样做完全错了吗

Index.cshtml(页面)

Index.cshtml.cs

public class IndexModel : PageModel
    {
        ApplicationDbContext mContext;
        public ImageGalleryModel ImageGallery;

        public IndexModel(ApplicationDbContext context)
        {
            mContext = context;
            ImageGallery = new ImageGalleryModel(mContext);
        }

        public void OnGet()
        {

        }
    }
public class ImageGalleryModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        public IList<ImageBE> ImageBE { get; set; }

        public ImageGalleryModel(Photiqo.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task OnGetAsync()
        {
            ImageBE = await _context.ImageBE.ToListAsync();
        }
    }
_ImageGallery.cshtml(部分)

_ImageGallery.cshtml.cs

public class IndexModel : PageModel
    {
        ApplicationDbContext mContext;
        public ImageGalleryModel ImageGallery;

        public IndexModel(ApplicationDbContext context)
        {
            mContext = context;
            ImageGallery = new ImageGalleryModel(mContext);
        }

        public void OnGet()
        {

        }
    }
public class ImageGalleryModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        public IList<ImageBE> ImageBE { get; set; }

        public ImageGalleryModel(Photiqo.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task OnGetAsync()
        {
            ImageBE = await _context.ImageBE.ToListAsync();
        }
    }
公共类ImageGalleryModel:PageModel
{
私有只读应用程序的bContext\u上下文;
公共IList ImageBE{get;set;}
公共ImageGalleryModel(Photiqo.Data.ApplicationDbContext上下文)
{
_上下文=上下文;
}
公共异步任务OnGetAsync()
{
ImageBE=wait_context.ImageBE.toListSync();
}
}

部分不应具有与之关联的页面模型文件。如果您有要执行的C代码,则应该考虑创建一个. 或者,您可以将
public IList ImageBE
属性移动到
IndexModel
中,并在那里的
OnGetAsync
方法中实例化它。然后,可以在零件上指定模型类型,并在当前执行的操作中使用标记辅助对象将其传递给零件:

_ImageGallery.cshtml(部分)

@model-IList
@foreach(模型中的var项目){
...

Hi Mike.谢谢你的回复!好的,很公平。但是即使我没有继承PageModel(或者按照你的例子),我也会得到同样的结果。我知道这个“应该”行得通。我尝试过在页面的ctor中启动列表,然后在*get()上,在model的ctor上,使用硬编码列表覆盖get属性。它在视图中总是以null结尾。显然,您在某个地方遗漏了某些内容…也许您可以查看此页上关于强类型部分的部分:看看它是否为您点亮了灯泡。是的,我同意。我将在今晚下班后从头开始。谢谢您的帮助nk和你的帮助:)
@model IList<ImageBE>

<table class="table">
    @foreach (var item in Model) {
    ...