I have this problem when I want to deserialize a jsonstring to a model in c#. For some reason it doesn't serialize all the properties correctly.
Some properties suddenly become 0 when it is a plain text or a shortdatestring.
Below you'll find my model and some screenshots.
public class AdvancedExportViewModel
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string IdentificationNumber { get; set; }
public string BirthDate { get; set; }
public string Address { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string DiveLicense { get; set; }
public string OtherLicenses { get; set; }
public string FederationNumber { get; set; }
public string MembershipDate { get; set; }
public string MedicalDate { get; set; }
public string MembershipType { get; set; }
public List<MemberCustomFieldViewModel> MemberCustomFields { get; set; }
public List<MembershipCustomFieldViewModel> MembershipCustomFields { get; set; }
}
public class MembershipCustomFieldViewModel
{
public int Id { get; set; }
public int CustomFieldId { get; set; }
public bool CustomFieldYearDependent { get; set; }
public string CustomFieldName { get; set; }
public CustomFieldsTypesDTO CustomFieldType { get; set; }
public string AccountId { get; set; }
public string Value { get; set; }
public bool BoolValue
{
get { return Value == "1"; }
set { Value = value ? "1" : "0"; }
}
}
The problem is situated in the "string Value" property of the CustomFieldViewModel.
This is my Json, and as you can see the Value property has has different values (string,shortdatetimestring and boolean as string) Json Here is my Deserialized Json on to my model As you can see, the values have become "0" deserialized model
How I deserialize my Json you'll find below:
List<AdvancedExportViewModel> model = JsonConvert.DeserializeObject<List<AdvancedExportViewModel>>(exportModel);
The object (exportModel) given in the DeserializeObject method is a Json string offcourse. I hope this is all clear.