更新对象时 JsonConvert PopulateObject 不会将空值填充为可为空的整数
JsonConvert PopulateObject when updating object doesnt populate null value to nullable integer
我正在尝试更新用户对象
这是我的模型:
public class User:BaseModel
{
[Display(Name="E-Posta"),Required(ErrorMessage ="Kullanıcı adı (Email) Zorunludur")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Email")]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage = "Hatalı E-Posta")]
public string Email { get; set; }
[Display(Name="Firma Ünvanı")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "FirmTitle")]
public string FirmTitle { get; set; }
[Display(Name = "Vergi Numarası")]
[StringLength(11, ErrorMessage = "Vergi Kimlik Numarası 10 veya 11 karakter olmalıdır", MinimumLength = 10)]
[JsonProperty(PropertyName = "VKN")]
public string VKN { get; set; }
[Display(Name="Alıcılar E-Posta")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "DocumentReceivers")]
public string DocumentReceivers { get; set; }
[Display(Name="Sap Lojistik Id")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "SAPLogisticId")]
public int? SAPLogisticId { get; set; }
[Display(Name="Parola"), Required(ErrorMessage = "Parola Zorunludur")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name="Rol"),Required(ErrorMessage ="Bu alan Gereklidir")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Role")]
public string Role { get; set; }
[Display(Name="Oluşturulma Tarihi")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "CreatedDate")]
public DateTime CreatedDate { get; set; }
}
我有一个名为 SAPLogisticId
的可空 int?
,我正在尝试将其设置为空值
var values = "{\"SAPLogisticId\":null}";
int key=4;
var user = userManager.Get(key);
JsonConvert.PopulateObject(values, user,new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver() { NamingStrategy = null }
});
但它没有设置 SAPLogisticId
的值,它什么都不做
其他属性有效
这是因为您正在使用 NullValueHandling = NullValueHandling.Ignore
。您告诉序列化程序忽略空值。如果您从 SAPLogisticId
和 的 JsonProperty
属性中删除它,JsonSerializerSettings
它将按预期工作。
我正在尝试更新用户对象
这是我的模型:
public class User:BaseModel
{
[Display(Name="E-Posta"),Required(ErrorMessage ="Kullanıcı adı (Email) Zorunludur")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Email")]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage = "Hatalı E-Posta")]
public string Email { get; set; }
[Display(Name="Firma Ünvanı")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "FirmTitle")]
public string FirmTitle { get; set; }
[Display(Name = "Vergi Numarası")]
[StringLength(11, ErrorMessage = "Vergi Kimlik Numarası 10 veya 11 karakter olmalıdır", MinimumLength = 10)]
[JsonProperty(PropertyName = "VKN")]
public string VKN { get; set; }
[Display(Name="Alıcılar E-Posta")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "DocumentReceivers")]
public string DocumentReceivers { get; set; }
[Display(Name="Sap Lojistik Id")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "SAPLogisticId")]
public int? SAPLogisticId { get; set; }
[Display(Name="Parola"), Required(ErrorMessage = "Parola Zorunludur")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name="Rol"),Required(ErrorMessage ="Bu alan Gereklidir")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Role")]
public string Role { get; set; }
[Display(Name="Oluşturulma Tarihi")]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "CreatedDate")]
public DateTime CreatedDate { get; set; }
}
我有一个名为 SAPLogisticId
的可空 int?
,我正在尝试将其设置为空值
var values = "{\"SAPLogisticId\":null}";
int key=4;
var user = userManager.Get(key);
JsonConvert.PopulateObject(values, user,new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver() { NamingStrategy = null }
});
但它没有设置 SAPLogisticId
的值,它什么都不做
其他属性有效
这是因为您正在使用 NullValueHandling = NullValueHandling.Ignore
。您告诉序列化程序忽略空值。如果您从 SAPLogisticId
和 的 JsonProperty
属性中删除它,JsonSerializerSettings
它将按预期工作。