首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未提供任何值时,List<Enum>无法反序列化

未提供任何值时,List<Enum>无法反序列化
EN

Stack Overflow用户
提问于 2019-11-05 23:43:40
回答 1查看 55关注 0票数 0

在类帐户中,属性行业是枚举类型行业的集合

一个账号可以属于多个行业。

代码语言:javascript
运行
复制
public class Account
{

[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; } 

}

Public enum Industry
{
AerospaceAirlines = 0,
            Agriculture = 1,
            Apparel = 2,
            Automotive = 3,
            Banking = 4,
            BioTechnology = 5,
            Chemicals = 6,
            Communications = 7,
            Construction = 8,
            Consultancy = 9,
            ConsumerDurables = 10,
            Education = 11,
}

新建账号时,不强制添加行业。当请求对象中未提供任何值时,此行业的属性将失败。

反序列化时出现错误“值不能为空。\r\n参数名称:源”

当默认绑定器试图填充accountModel对象时,就会发生故障。

代码语言:javascript
运行
复制
[HttpPost]
        public async Task<IActionResult> Post([FromBody]AccountModel accountModel)
        {
            if (accountModel == null)
            {
                throw new ApiException(ApplicationErrorCode.FieldRequiredError, "request cannot be empty");
            }
       }

在没有提供行业的情况下,如何允许发件人发送请求?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-05 23:46:54

更改:

代码语言:javascript
运行
复制
public class Account
{

[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; } 

}

至:

代码语言:javascript
运行
复制
public class Account
{

[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; } = new List<Industry>();

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58714794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档