ASP .NET Core Razor:模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数

编程入门 行业动态 更新时间:2024-10-13 18:20:18
本文介绍了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:模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数

本文发布于:2023-11-16 15:22:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   能为   绑定   抽象   有无

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!