TextBoxFor Decimal Member without decimal places


I got a decimal member in my viewmodel and get populated from the database (say 55.5, or 100.00)

In my view I use TextBoxFor<> for this member.

Is there any way to have a number (it’s actually a percentage) like 100.00 display as 100 instead, and 55.50 as 55.5?

Probably a stupid question (really tired here)

The easiest way is via an HtmlExtension method. This way it is accessible in several places. Just wrap TextBoxFor in your new method as something like FormattedTextBoxFor.

Your next option is do it in your view model, before it’s rendered in the view, so that model.Value is already formatted, or as a separate model.FormattedValue, so the original is still available.

How about [value].ToString(“F0”)?