简体   繁体   English

停止格式化数字的 Asp.Net Core 标签助手

[英]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":当 asp-for 格式化我的 output 时,我遇到了这种情况,我输入了 5,但它显示我“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.有没有办法阻止 asp.net 格式化我的数字 - 保持 5 不变,无需添加任何内容。

<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..当您使用 Html.DisplayFor 时,会考虑 DisplayFormat,我不知道如何强制数字采用特定格式,如果您自己没有将其更改为字符串,或者可能有..

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我仍然认为这不能解决小数问题,它将是 4.00 而不是 4,00

If you don't want decimals, on view model use int instead or decimal如果您不想要小数,请查看 model 使用 int 代替或小数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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