I did some more global solution, display blank for:
- integer: 0
- decimal: 0.0
- string: "0x0" or "0x0x0"
Extension class:
public static class DataTypesExtension
{
static string[] Keywords = { "0", "0.0", "0.00", "0.000", "0.0000", "0,0", "0,00", "0,000", "0,0000", "0x0", "0x0x0" };
public static string BlankIfZero(this object n)
{
string ret = string.Empty;
if (n == null)
return ret;
string sn = n.ToString().Replace(" ", "");
if (!DataTypesExtension.Keywords.Contains(sn))
ret = n.ToString();
return ret;
}
}
Display template:
@model object
@using MyProject.Extensions;
@{
string val = Model.BlankIfZero();
@val;
}
And in View Model:
[UIHint("BlankIfZero")]
public int? MyIntProperty { get; set; }
[UIHint("BlankIfZero")]
public decimal? MyDecimalProperty { get; set; }
[UIHint("BlankIfZero")]
public string MyStringProperty { get; set; }