Asp.net MVC 小数符号问题
我有一个具有特定视图的 Asp.net MVC 应用程序,该视图具有带有 float
属性的模型。
我的服务器区域设置表示 ,
是十进制符号,当我编辑模型并在文本框中输入例如 7,5 并将其发布到服务器时,它工作正常。默认模型绑定器能够按预期将此值绑定到七个半。
但是,当我使用 <%= this.Model.FloatValue %>
显示相同的值时,小数符号将转换为 .
这意味着 <% = %>
显然忽略了服务器区域设置。
所以。那我该如何解决这个问题呢?应该使用哪个区域设置?服务器系统区域设置表示十进制符号为 ,
或浏览器区域设置设置为 en-gb
,这意味着 .
是十进制符号。
反正。我只是想让它可靠地工作。
一些代码:
我的控制器操作:
public ActionResult Settings()
{
Settings result = this.Service.GetActiveSettings();
return View(result);
}
[HttpPost]
[HandleModelStateException]
public ActionResult Settings(Settings data)
{
if (!this.ModelState.IsValid)
{
throw new ModelStateException(); // my custom exception which isn't important here
}
Settings result = this.Service.SaveSettings(data);
return Json(result);
}
第二个是使用 $.ajax()
异步调用的。
部分视图的相关部分:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Settings>" %>
<div class="data">Float value: <strong><%= this.Model.FloatValue %></strong></data>
<div class="data">Integer value: <strong><%= this.Model.IntValue %></strong></data>
...
任何我的模型类:
/// <summary>
/// Represents application specific settings.
/// </summary>
public class Settings
{
/// <summary>
/// Gets or sets the integer value.
/// </summary>
/// <value>Integer value.</value>
[Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRequired")]
[Range(0, 365*24, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRange")]
public int IntValue { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRequired")]
[Range(.5, 24.0, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRange")]
public float FloatValue { get; set; }
}
正如您所看到的,这里没有什么异常。哦,顺便说一句:FloatValue
上的范围验证也不起作用。
I have an Asp.net MVC application with a particular view that has a model with a float
property.
My server locale says that ,
is decimal symbol and it works fine when I edit my model and enter for instance 7,5 in the text box and post it to server. Default model binder is able to bind this value as expected to seven and a half.
But when I display this same value using <%= this.Model.FloatValue %>
, decimal symbol is converted to .
which means that <%= %>
obviously ignores server locale settings.
So. How do I solve this problem then? Which locale should be used? Server system locale which says that decimal symbol is ,
or browser locale setting that's set to en-gb
, which means that .
is decimal symbol.
Anyway. I just want this to work reliably.
Some code:
My controller actions:
public ActionResult Settings()
{
Settings result = this.Service.GetActiveSettings();
return View(result);
}
[HttpPost]
[HandleModelStateException]
public ActionResult Settings(Settings data)
{
if (!this.ModelState.IsValid)
{
throw new ModelStateException(); // my custom exception which isn't important here
}
Settings result = this.Service.SaveSettings(data);
return Json(result);
}
The second one is called asynchronously using $.ajax()
.
Relevant part of the partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Settings>" %>
<div class="data">Float value: <strong><%= this.Model.FloatValue %></strong></data>
<div class="data">Integer value: <strong><%= this.Model.IntValue %></strong></data>
...
Any my model class:
/// <summary>
/// Represents application specific settings.
/// </summary>
public class Settings
{
/// <summary>
/// Gets or sets the integer value.
/// </summary>
/// <value>Integer value.</value>
[Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRequired")]
[Range(0, 365*24, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "QuotaRange")]
public int IntValue { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRequired")]
[Range(.5, 24.0, ErrorMessageResourceType = typeof(Resources.Settings), ErrorMessageResourceName = "WorkdayRange")]
public float FloatValue { get; set; }
}
As you may see, there's nothing unusual here. Oh and BTW: Range validation on FloatValue
doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试禁用基于客户的文化?您可以在 web.config、system.web 部分找到它。
Did you try disabling Client Based Culture? You can find it in web.config, system.web section.
转换为字符串时使用自定义
CultureInfo
最后,我使用了
ToString()
方法并提供了适当的文化信息。我必须提供更多的代码,但它按预期工作:当我需要在同一视图上多次使用此文化信息时,我只需创建一个视图变量并存储它,然后在每个实例中重用它。
Using custom
CultureInfo
when converting to stringIn the end I used
ToString()
method and provide appropriate culture info. I had to provide a bit more code, but it works as expected:And when I need to use this culture info multiple times on the same view I simply create a view variable and store it and then reuse it with every instance.