Convert long number as string in the serialization

前端 未结 5 2174
一整个雨季
一整个雨季 2020-12-14 09:47

I have a custom made class that use a long as ID. However, when I call my action using ajax, my ID is truncated and it loses the last 2 numbers because javascript loses prec

5条回答
  •  不知归路
    2020-12-14 10:48

    Depending on your case, you could potentially use getters and setters to masquerade the property as a string during JSON serialization.

    public class Money
    {
        [JsonIgnore]
        public decimal Money { get; set; }
    
        [JsonProperty("money")]
        public string MoneyAsString
        {
            get { return Money.ToString("0.00"); }
            set { Money = decimal.Parse(value); }
        }
    }
    

提交回复
热议问题