如何将视图中的双精度数值传递到控制器中

huangapple go评论69阅读模式
英文:

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&#39;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[] { &quot;en-GB&quot; };
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(&quot;en-GB&quot;);
cultureInfo.NumberFormat.NumberDecimalSeparator = &quot;.&quot;;

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.

huangapple
  • 本文由 发表于 2023年4月20日 00:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定