Duplicate 'DisplayFormat' Attribute in ViewModel

532 Views Asked by At

I would like to display a value from ViewModel in the view. If value is null, it will be displayed as "--", otherwise it will be rounded to 2 decimal places. The following code in my ViewModel gave me "Duplicate DisplayFormat Attribute" error.

[Display(Name = "Number")]
[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "--")]
[DisplayFormat(ConvertEmptyStringToNull = false, DataFormatString = "{0:n2}")]
public Nullable<float> C_Targets { get; set; }

I was wondering if there is a way that I can combine these two cases in one displayformat attribute? Thanks!

0

There are 0 best solutions below