I have a property of decimal type for a grid -
[DisplayFormat(DataFormatString = "{0:0.000000}", ApplyFormatInEditMode = true)]
public decimal? UnitPrice { get; set; }
I want [DisplayFormat to be custom to accept different values for different rows like -
UnitPrice for row 1 ~ only show 2 decimals {0:0.00}
UnitPrice for row 2 ~ show 4 decimals - {0:0.0000}
The values for after decimal are coming from database and I'm storing them in the model as another property like -
public int UnitsDecimals { get; set; }
I'm using editor & display template already. Should I create custom htmlhelper? If yes, how can I pass the value of UnitsDecimal to the custom helper?
You can add additional property in you view model to indicate the source of you data.
public class ViewModel {
public decimal? UnitPrice { get; set; }
//This Changes as per source
public string PriceNumberFormatString{get;set;}
}
You can have elaborate PriceNumberFormatString getter setter for different values of decimal inputs from Data source..
Then in View you can use value of PriceNumberFormatString to manipuate view of UnitPrice. The manipulation depends on your UI library .