Data Annotation for currency format not working

前端 未结 2 603
遥遥无期
遥遥无期 2021-01-27 15:34

In my ASP.NET MVC Core web project on VS2015, the following model is displaying data as, e.g., 15481 instead of $15,481 even though I\'m using [D

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-27 16:06

    The currency annotation can be used. However it is just telling MVC which display or editor template to use. As We said current template uses the system currency. You would have to provide custom editor template or display template and some other way to determine the currency symbol to display.Look here at how to provide your own implementations

    Try using this

     [DisplayFormat(DataFormatString = "{0:C0}")]
    

    Example

    public class Sales
    {
        [Key]
        public int SalesId { get; set; }
    
        [DisplayFormat(DataFormatString = "{0:C0}")]
        public float? SaleAmount { get; set; }
    }
    

    Check here for more details

提交回复
热议问题