Unable to cast object of type 'System.Int32' to type 'System.String'.

最近在研究telerik,本来好好的,添加了一个可为空的Int32字段后总是报错,最初以为空字段需要单独处理些什么,但后来翻箱倒柜,终于从:https://www.cnblogs.com/Godric/p/10111594.html发现原来是model中写法错了,跟参考博客一样

System.InvalidCastException: The field of type System.Int32 must be a string, array or ICollection type.
   at System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid(Object value)
   at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
   at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
   at Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsModelValidator.Validate(ModelValidationContext validationContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.ValidateNode()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitSimpleType()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata metadata, String key, Object model, Boolean alwaysValidateAtTopLevel)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, String prefix, Object model, ModelMetadata metadata)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.EnforceBindRequiredAndValidate(ObjectModelValidator baseObjectValidator, ActionContext actionContext, ParameterDescriptor parameter, ModelMetadata metadata, ModelBindingContext modelBindingContext, ModelBindingResult modelBindingResult)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<g__Bind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: commonFile=common; theme=metro; .AspNetCore.Culture=c%3Dzh-Hans%7Cuic%3Dzh-Hans; _ga=GA1.1.1885739615.1533174723; .AspNet.Consent=yes; .AspNetCore.Antiforgery._c5EZJxRN8Y=CfDJ8BbosGIuwFlNjQMCkDzkXwM2VLpXKRMZJIh7IOYm6B3VuS86rWFj3hx5hWu6VMfFjpOL2RYJjWqTK3eGxPmXg2-LZSMLHj3qsiRUTMyHMPysPnI-mJWjHlVacQVQJpCOvQx64ioyfIhwqfen8NXaA70; .AspNetCore.Antiforgery.6Pkf5otFOyQ=CfDJ8BbosGIuwFlNjQMCkDzkXwMXcvkfHLPnwzGBw9l54Dwmjm_lynWmcIXTlf7u69PZNf4UI_N9nFHNTFPrwfI3YivIMDi57OJY4A4Q1NVmLcQZolVndXuPogKvA5o3nAOQVZ-gk8BnzSfJZPm89PFfU7A; .AspNetCore.Session=CfDJ8BbosGIuwFlNjQMCkDzkXwOYRLky4QczqHKwfISFBjBNpmhpLEU%2BmW58V41LqvuZ9sy2A6Onwg8srZizeTzLZfWT%2BAyYL5LqYq7syqCiUJbYMeVqepyGXot1upTwh1c0rVXSgiqU%2FYlxGjDmNGij8s3FoANpxwjxWzqxcPF3Merc; .AspNetCore.Cookies=CfDJ8BbosGIuwFlNjQMCkDzkXwNNxuMUbkl1IJnuh5xgjRbFDWQC-eAf1Zwv9siRmnwyl-HJdJbASLdhbxHL4Xri2qgrNZO_ncJJNINjuFlWHTfNOdue5f2GW1jVvajjTggOEGKYfg_2LtW2xMi-jFYflN48PBPbCPNyfDLVhn8rEU9GkbvGaR-vvZ_pXw8iS-gpoWG8ET9zrcPecPxXFPMbYsbKuJg6UffI3DeDHsz3SVOmPcByDGcjztQlrbwGDo9INJeUTxx-5N1yzW23yrSvnE_SWGyVXtYPT7x7KyVmCq3px26E32yZqUItwuiVElFF__nPjb-lbDmEWFOD0rPsH2RS3QlUquBTYSQL_FlyYYVV1ertJqin1vx1pYYoPkTb6D4cw6NK-2UszO4kg2r65KB5xpog76i8PszoO

 关键错误点Unable to cast object of type 'System.Int32' to type 'System.String'.提示是无法将“System.Int32”的对象强制转换为“System.String”。于是我就找了所有使用Int的地方,终于发现

1         /// 
2         /// 排序数字
3         /// 
4         //[MaxLength(10)]这个不能加,自己做的代码生成器添加的
5         public Int32? SortId {get;set;}

在一个基类中有个Int类型的ID,不小心加上了MaxLength属性。int类型是不能添加MaxLength、StringLength属性的,去掉之后就可以正常访问了。

你可能感兴趣的:(Unable to cast object of type 'System.Int32' to type 'System.String'.)