Validation 服务器端验证问题-浮点值

Validation 服务器端验证问题-浮点值,validation,asp.net-core,entity-framework-core,razor-pages,Validation,Asp.net Core,Entity Framework Core,Razor Pages,我的应用程序中的数据验证有问题。 我正在使用Razor Pages和.NETCore3.0框架以及EFCoreORM。 在我的模型中,我有两个属性: public float WireCrosssection { get; set; } public float CableLength { get; set; } 在第页,我为他们提供了输入: <div class="form-group"> <label asp-for="Cable.WireCros

我的应用程序中的数据验证有问题。 我正在使用Razor Pages和.NETCore3.0框架以及EFCoreORM。 在我的模型中,我有两个属性:

public float WireCrosssection { get; set; }
public float CableLength { get; set; }
在第页,我为他们提供了输入:

    <div class="form-group">
        <label asp-for="Cable.WireCrosssection"></label>
        <input class="form-control" asp-for="Cable.WireCrosssection" />
        <span class="text-danger" asp-validation-for="Cable.WireCrosssection"></span>
    </div>
    <div class="form-group">
        <label asp-for="Cable.CableLength"></label>
        <input class="form-control" asp-for="Cable.CableLength" />
        <span class="text-danger" asp-validation-for="Cable.CableLength"></span>
    </div>

客户端验证已打开,此验证不会报告表单问题,但服务器端验证会报告表单问题(ModelState.IsValid为false)。 该数字带有点(“.”)。
有什么建议吗?

确保已在视图中添加验证脚本

@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
下面是一个简单的演示,如下所示:

1.型号:

public class Test
{     
    public float WireCrosssection { get; set; }
    public float CableLength { get; set; }
}
2.意见:

@model Test
<form asp-action="Index">
    <div class="form-group">
        <label asp-for="WireCrosssection"></label>
        <input class="form-control" asp-for="WireCrosssection" />
        <span class="text-danger" asp-validation-for="WireCrosssection"></span>
    </div>
    <div class="form-group">
        <label asp-for="CableLength"></label>
        <input class="form-control" asp-for="CableLength" />
        <span class="text-danger" asp-validation-for="CableLength"></span>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
4.结果:


参考资料:

好的,我解决了这个问题。问题是服务器端和客户端的区域性不匹配。客户端验证是以“en-US”文化编写的。要在ASP.NET核心应用程序上设置区域性,需要在
Startup.cs
类中的
Configure
方法中添加以下代码:

var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

你解决问题了吗?如果我的回答有帮助,你能吗?我还没有解决这个问题。现在我知道,当客户端验证被禁用时,我可以将带有逗号(1,3)的值发送到服务器,模型是有效的,但当值带有点(1.3)时,服务器端验证会出错。是否有机会在服务器端更改此设置?把逗号改成点?
var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;