英文:
How to pass double value from view into controller
问题
Entity:
public class Material
{
public int Id { get; set; }
public string? Name { get; set; }
public int? Unit { get; set; }
public double? Count { get; set; }
public double? Amount { get; set; }
public double? Cost { get; set; }
}
Controller:
public IActionResult Edit(int id, Material material)
{
...
}
View:
<div class="form-group">
<label asp-for="Count" class="control-label"></label>
<input asp-for="Count" class="form-control" />
<span asp-validation-for="Count" class="text-danger"></span>
</div>
On the internet, I came across the following:
<configuration>
<system.web>
<globalization
enableClientBasedCulture="true"
culture="en-GB"/>
</system.web>
</configuration>
It needs to be placed into Web.config, but I don't have this file in my solution.
ASP.NET Core MVC (.NET 6)
Please note that I have translated the code sections you provided.
<details>
<summary>英文:</summary>
Entity:
public class Material
{
public int Id { get; set; }
public string? Name { get; set; }
public int? Unit { get; set; }
public double? Count { get; set; }
public double? Amount { get; set; }
public double? Cost { get; set; }
}
Controller:
public IActionResult Edit(int id, Material material)
{
...
}
View:
<div class="form-group">
<label asp-for="Count" class="control-label"></label>
<input asp-for="Count" class="form-control" />
<span asp-validation-for="Count" class="text-danger"></span>
</div>
If I try to pass `10.2`
[![enter image description here][1]][1]
[![enter image description here][2]][2]
If I try to pass `10,2`
[![enter image description here][3]][3]
On the internet I came across the following
<configuration>
<system.web>
<globalization
enableClientBasedCulture="true"
culture="en-GB"/>
</system.web>
It need to past into Web.config, but i don't have this file in my solution
ASP.NET Core MVC (.NET 6)
[1]: https://i.stack.imgur.com/KwQDZ.png
[2]: https://i.stack.imgur.com/MCm7v.png
[3]: https://i.stack.imgur.com/kmQPy.png
</details>
# 答案1
**得分**: 2
以下是已翻译的部分:
一种选项是在应用程序端启用请求本地化:
```csharp
var app = builder.Build();
var supportedCultures = new[] { "en-GB" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
或者,您可以在应用程序启动时手动操作默认文化:
var cultureInfo = new CultureInfo("en-GB");
cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
阅读更多 - 在本地化的ASP.NET Core应用程序中为每个请求实施选择语言/文化策略。
英文:
One option is to enable request localization on the app side:
var app = builder.Build();
var supportedCultures = new[] { "en-GB" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
Or you can just manually manipulate the default culture on app start:
var cultureInfo = new CultureInfo("en-GB");
cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
Read more - Implement a strategy to select the language/culture for each request in a localized ASP.NET Core app.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论