【问题标题】:Binding to Business object containg Enum Type as one of the properties绑定到包含枚举类型作为属性之一的业务对象
【发布时间】:2021-09-03 14:53:39
【问题描述】:

我有一个名为“Lead”的班级,如下所示

public class Lead
{
   
    public int Id { get; set; }
    [Required]
   
    public string FirstName { get; set; }
    [Required]
   
    public string LastName { get; set; }
    [Required]
  
    public string Email { get; set; }
    [Required]
  
    public string Mobile { get; set; }
    [Required]
    public string LocationString { get; set; }
    public string Communication { get; set; }
  
    public Status Status { get; set; }
  
    public LocationType LocationType { get; set; }
}

public enum Status
{
    Contacted=1,Ceated=2
}

public enum LocationType
{
    City=1,State=2,Country=3
}

我要求正文如下

{
"first_name": "Dixi",          
"last_name": "Payal",          
"mobile": "7412589632",          
"email": "asd@email.com",      
"location_type": "1",        
"location_string":  "India"  
} 

用户将传递位置类型枚举的字符串。默认模型绑定器为此引发错误。 谁能告诉我如何正确检查枚举值并将其绑定到对象的属性

【问题讨论】:

    标签: asp.net-core asp.net-core-3.1


    【解决方案1】:

    尝试以整数形式发送"location_type": 1,。 ASP.NET Core 3.1 默认通过其整数值绑定枚举。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多