Can't set display format to 2 decimal places when using mvc6 grid

戏子无情 提交于 2021-01-27 12:23:25

问题


I need to display some average sums of currency values in my .net core asp mvc project.

I am using mvc 6 grid to display the data, and I have tried these solutions:

[DisplayFormat(DataFormatString = "{0:C}")]
public double? AverageCost { get; set; }

[DisplayFormat(DataFormatString = "{0:#.####}")]
public double? AverageCost { get; set; }

[RegularExpression(@"^\d+\.\d{0,2}$")]
public double? AverageCost { get; set; }

But still my values are displayed with several decimal places:

Am I missing something?

I know I can format the columns using the mvc 6 grid, but is there not a way of doing this in the ViewModel?

Note that this is NOT a duplicate of the suggested question.. If you actually read the questions you will see that.


回答1:


In your first line you missed the decimal points after 'C'. it should have been [DisplayFormat(DataFormatString = "{0:C2}")]




回答2:


I was able to do this by using the Formatted method and passing in the value {0:N} in the view itself:

columns.Add(model => model.AverageCost).Titled("AverageCost").Formatted("{0:N}");

I found the answer by getting the format string from here and finding the Formatted method used here

This is perfect for me as it displays the values as currency but does not show the currency symbol as {0:C} does



来源:https://stackoverflow.com/questions/52873052/cant-set-display-format-to-2-decimal-places-when-using-mvc6-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!