You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML数字输入框整数可预填充,小数无法显示问题求助

Troubleshooting Decimal Value Not Showing in ASP.NET Core Number Input

Hey there! Let's figure out why your pre-filled decimal value isn't showing up in the number input—integers working fine but decimals not is a classic sign of culture/formatting mismatch, especially since your label is in French (Temps prévu) which uses commas for decimal separators. Here are the most likely fixes:

1. Fix the Value Formatting with Invariant Culture

The asp-for tag helper might be formatting the decimal value using your server's default culture (e.g., French, which uses , instead of .). Most browsers expect number inputs to use a dot (.) as the decimal separator by default, so they'll ignore values with commas entirely.

Add an explicit value attribute that formats the number with the invariant culture to force a dot separator:

<dd class="col-sm-9 detailsElements">
    <input asp-for="ExceptedTime" 
           class="form-control" 
           type="number" 
           step=".01" 
           required
           value="@Model.ExceptedTime.ToString(System.Globalization.CultureInfo.InvariantCulture)"/>
    <span asp-validation-for="ExceptedTime" class="text-danger"></span>
</dd>

This ensures the value is rendered as 2.4 instead of 2,4, which the browser's number input will recognize and display correctly.

2. Verify Your Model Field Type

Double-check that your ExceptedTime property in the model is a decimal or double type, not an integer type like int or long. If it's an integer, the decimal value would get truncated before it even reaches the view:

// Correct type (supports decimals)
public decimal ExceptedTime { get; set; }

// Wrong type (truncates decimal values)
public int ExceptedTime { get; set; }

3. Test with a More Flexible Step Attribute

While step=".01" is correct for two decimal places, some browsers can be picky about value precision matching the step. Try temporarily setting step="any" to see if the value displays—this disables strict step validation and lets any decimal value show up:

<input asp-for="ExceptedTime" class="form-control" type="number" step="any" required ... />

If this works, you can switch back to step=".01" once the formatting issue is fixed, since it's better for enforcing input precision.

4. Check Client-Side Validation Settings

If you're using jQuery Validate (which ASP.NET Core uses by default for client validation), it might be enforcing culture-specific rules that conflict with the input's expected format. Add this script to your view or layout to make validation support both dot and comma separators:

$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
};

内容的提问来源于stack exchange,提问作者VickeS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:07:05