gpt4 book ai didi

c# - MaxLength + Asp.net Web API - 出错

转载 作者:行者123 更新时间:2023-11-30 20:56:17 24 4
gpt4 key购买 nike

我的 PriceDM 类中有这个属性。

 [MaxLength(8)]
public decimal Price { get; set; }


public void AddPrice([FromBody]PriceDM vm)
{
}

虽然我一直收到这个错误

{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.Decimal' to type 'System.Array'.","ExceptionType":"System.InvalidCastException","StackTrace":" at System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid(Object value)\r\n at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)\r\n at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)\r\n at System.Web.Http.Validation.Validators.DataAnnotationsModelValidator.Validate(ModelMetadata metadata, Object container)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ShallowValidate(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(ModelMetadata metadata, ValidationContext validationContext)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n
at System.Web.Http.Validation.DefaultBodyModelValidator.Validate(Object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, String keyPrefix)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.<>c_DisplayClass1.b_0(Object model)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass361.<>c__DisplayClass38.<Then>b__35()\r\n
at
System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass49.<ToAsyncVoidTask>b__48()\r\n
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func
1 func, CancellationToken cancellationToken)"}

我不能使用带小数的 MaxLength 吗?

最佳答案

如果你看Microsoft documentation您将看到 MaxLength 属性的以下说明。

Specifies the maximum length of array or string data allowed in a property.

我会尝试 Range attribute .

关于c# - MaxLength + Asp.net Web API - 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com