c# - How do I format decimal as Percentage in EF Code First rendered in Razor TextBoxFor? -
i have property:
public decimal myproperty { get; set; }
and here render:
@html.textboxfor(m => m.myproperty , new { @class = "percentage" })
how do percentage?
you decorate view model property [displayformat]
attribute allowing specify format:
[displayformat(applyformatineditmode = true, dataformatstring = "{0:p2}")] public decimal myproperty { get; set; }
and in view:
@html.editorfor(x => x.myproperty)
but careful because displayformat attribute (as name suggests) used displaying purposes. not used default model binder. when user submits value posting form chances validation error because example 0.45%
not valid decimal value. have illustrated in this post
how custom model binder defined use format defined displayformat attribute when binding value.
Comments
Post a Comment