asp.net-core-mvc - 文化不一致 - 剃刀视图和视图模型之间的模型绑定中忽略了小数点分隔符
问题描述
我的程序中有以下行为:
十进制变量的用户输入
a) jquery 验证关闭:
1) 如果用户使用逗号作为小数分隔符,则该值正确存储在 ViewModel 中
2) 如果用户使用点作为小数分隔符,则该值乘以 100(好像有没有小数分隔符)
B)jquery验证打开:
1)我得到一个错误,必须提供一个数字
2)与A2相同的问题)
但是,如果我在视图中显示 ViewModel 的十进制值,则默认情况下会显示一个点作为小数分隔符。
这种不一致让我感到困惑,我想实现一致的行为,但不幸的是我不知道我真正在寻找什么。
该网站将本地化为德语和意大利语。到目前为止,本地化工作没有任何问题。这是我的
启动.cs
namespace XXX
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Added - uses IOptions<T> for your settings.
services.AddOptions();
// Added - Confirms that we have a home for our DemoSettings
services.Configure<DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/Index", "?i_statusCode={0}");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("de"),
new CultureInfo("it"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("de"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=de}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "defaultLocalized",
template: "{culture=de}/{controller=Contract}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "error",
template: "Error/Index",
defaults: new { controller = "Error", action = "Index", culture = "de" });
mvcRoutes.MapRoute(
name: "default",
template: "{*catchall}",
defaults: new { controller = "Home", action = "Index", culture = "de" });
});
});
});
}
}
}
解决方案
推荐阅读
- tensorboard - 将多个 logdirs 传递给 Tensorboard dev
- html - 当窗口缩放时,如何将我的菜单项与 div 完美对齐?| HTML | CSS
- swift - Mac M1获取IOMobileFramebufferUserClient接口
- python - 在 Django Rest Framework 中从 WSGI 迁移到 ASGI 后无法运行异步函数
- r - 使用 rmarkdown::render 在循环内打印 gt 表不起作用
- javascript - ENOENT:Node.js 脚本 fs.rename,用于将文件从 1 个文件夹分割和移动到多个其他文件夹
- android - 来自 api 的 kotlin 布尔改造
- angular - 我在 highcharts 上遇到错误。像“错误类型错误:无法读取未定义的属性‘系列’”
- reactjs - react-router-dom 中的 useLocation 钩子如何工作?
- oop - 这两种方法有什么区别[Smalltalk Best Practice Patterns - Kent Beck]?