0

My Html.EditorFor value is 0.00 instead of empty for decimal property as follows:

Screenshot

How can I set it to empty instead of 0.00?

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
PLB
  • 265
  • 2
  • 17
  • Please check [this answer](https://stackoverflow.com/a/19676387/1955268). Try to add DisplayFormat attribute to your model as suggested by Sel. – prinkpan Dec 26 '18 at 10:41

2 Answers2

2

This is because decimal is non-nullable type and default value for decimal is 0.00 if you don't provide any explicit value. And in your case when you are initializing your model class you are not providing any value to it and that's why its taking default value 0.00.

You can remove the 0.00 from your form input field by simply as follows:

public class YourModelClass
{
    ..........
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")]
    public decimal CurrentSoftMin { get; set; }
    ..........
}
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
-1

In your code change code to this [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")] decimal InterestRate { get; set; }

and in view

@Html.TextBoxFor(model => model.InterestRate)
PLB
  • 265
  • 2
  • 17