プロパティを持つモデルを持つ特定のビューを持つAsp.netMVCアプリケーションがありfloatます。
私のサーバーロケールはそれ,が小数点記号であると言っており、モデルを編集してテキストボックスにたとえば7,5と入力し、サーバーに投稿すると正常に機能します。デフォルトのモデルバインダーは、期待どおりにこの値を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);
}
2つ目は、を使用して非同期的に呼び出されます$.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ません。