ASP .NET Core Razor:模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数 [英] ASP .NET Core Razor: Model bound complex types must not be abstract or value types and must have a parameterless constructor

查看:131
本文介绍了ASP .NET Core Razor:模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的模型中有这样的属性:

If I have a property like this in my model:

    [BindProperty]
    public IPagedList<Product> Products { get; set; }

然后当我尝试发布时,出现此错误:

then when I try to post, I get this error:

An unhandled exception occurred while processing the request.
InvalidOperationException: Could not create an instance of type 'X.PagedList.IPagedList`1[Data.Models.Product, Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the 'Products' property to a non-null value in the 'Areas.Catalog.Pages.ProductListModel' constructor.

错误表明我可以在构造函数中将属性设置为非null值,所以我尝试在构造函数中执行此操作:

The error says I can set the property to a non-null value in the constructor, so I try to do this in the constructor:

Products = new PagedList<Product>(Enumerable.Empty<Product>(), 1, 10);

但是我遇到了同样的错误.

But I get the same error.

推荐答案

当我删除[BindProperty]时,它会起作用.我的印象是我需要在Razor页面上绑定属性,但我想不是吗?

When I remove [BindProperty] it works. I was under the impression I needed that to bind a property on a Razor page, but I guess not?

这篇关于ASP .NET Core Razor:模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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