简体   繁体   中英

Stop Asp.Net Core tag helpers formatting numbers

I have the situation when the asp-for formats my output, I put 5 but it displays me "5,00":

在此处输入图像描述

As I am in France, it puts the comas, and then does not validate the input (because of format I suppose).

Is there a way to stop asp.net formatting my numbers - leave 5 as is, no neeed to add something to it.

<label asp-for="X" class="control-label"></label>
<input asp-for="X" class="form-control" />
<span asp-validation-for="X" class="text-danger"></span>

I added

[DisplayFormat(ApplyFormatInEditMode = false)]
public decimal X { get; set; }

but this does not help.

I need to use decimals in my fields, but why it does put the comas by itself, then says "invalid format"?

DisplayFormat is considered when you use Html.DisplayFor, I don't know a way how is possible to enforce a number to be in a specific format it you are not changing it yourself to string, or maybe there is..

Another approach is to change the culture, US or invariant

 public ActionResult Index()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture =CultureInfo.InvariantCulture;
            return View();
        }

or in view (not very fancy)

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo.InvariantCulture;
Html.DisplayFor(model => model.whatever)

Still I don't think this solved the decimals problem, it will be 4.00 instead of 4,00

If you don't want decimals, on view model use int instead or decimal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM