UяošKoт
UяošKoт

Reputation: 474

Input shows blank instead of zero

I have a C#.NET MVC4 application. It has an entity with a decimal property (somewhere within a connected MSSQL database):

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:.}")]
public decimal Fines { get; set; }

and then I display it as

@Html.EditorFor(model => model.Fines)

If I save it with value 1, 2, 3, ... and then open the record for editing, the text input value is set to the saved value and everything works fine. However if the value was 0, (it is stored to the database, but) it does not show as "0" string in text box but instead as an empty string "", which causes validation errors since this is a required field (and 0 is a valid value).

What am I doing wrong?

Upvotes: 3

Views: 3683

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103525

Your format string is a bit weird.. I'm not sure what format you are going for there. Try this:

DataFormatString = "{0}"

Or if you want it formatted as currency,

DataFormatString = "{0:C}"

Note: Just tested this:

string.Format("{0:.}", (decimal)0)

It returns an empty string.

See the msdn pages on standard and custom numeric format strings.

Upvotes: 2

Related Questions