Thomas Adrian
Thomas Adrian

Reputation: 3636

Show SQL decimal type without decimal if there are no decimals

I am using ASP.NET Core v2,

See picture showing SQL data type and the code in the model

I do not want to display decimal if there are no decimals

The following html is used to show the input on web page

<input tabindex=@ordTab asp-for="@Model.OrderLineList[i].NoInvoAb" />

below is SQL

enter image description here

below is the model

I have tried many different display formats/dataformatstring but it does not seem to matter what I use, like the formatting does not seem to apply at all

[Required]
[DisplayFormat(DataFormatString = "{0:0}")]
[Display(Name = "Ordered")]
public decimal NoInvoAb { get; set; }

This is my result which is wrong, if the value is 2, it should display 2 and not 2,00

enter image description here

what am I doing wrong

Upvotes: 0

Views: 78

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

DisplayFormat by default is for display. Here, it looks like the value is in an input. If you want the format apply there as well, then you need to add ApplyFormatInEditMore = true to your DisplayFormat attribute.

[Required]
[DisplayFormat(DataFormatString = "{0:0}", ApplyFormatInEditMode = true)]
[Display(Name = "Ordered")]
public decimal NoInvoAb { get; set; }

Upvotes: 1

Related Questions