UpdateModelまたはTryUpdateModelで$ 1,200.00などの金額または通貨の書式設定された値を小数点以下に解析する方法はありますか?通貨フォーマットの値を指定したTryUpdateModel?
5
A
答えて
3
カスタムモデルバインダーを使用してください。
+0
私が望んでいたほどシンプルでエレガントではありませんでしたが、実際にはうまくいきましたので、ありがとうございました。私は私の微調整されたバインダーを投稿します。 –
1
これらのメソッドのいずれかを呼び出す前に値を解析することはできますか?その場合は、次の方法を使用してください。
var provider = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
provider.CurrencySymbol = "$";
var x = decimal.Parse(
"$1,200",
NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
provider);
2
回答は彼のリンクはこれを行うためにベースを提供してくれまし以来、フレディリオスに授与されましたが、コードはいくつかは、修理する必要があった。
// http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/
public class MoneyParsableModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
object result = null;
// Added support for decimals and nullable types - c.
if (
bindingContext.ModelType == typeof(double)
|| bindingContext.ModelType == typeof(decimal)
|| bindingContext.ModelType == typeof(double?)
|| bindingContext.ModelType == typeof(decimal?)
)
{
string modelName = bindingContext.ModelName;
string attemptedValue = bindingContext.ValueProvider[modelName].AttemptedValue;
// Depending on cultureinfo the NumberDecimalSeparator can be "," or "."
// Both "." and "," should be accepted, but aren't.
string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
string alternateSeperator = (wantedSeperator == "," ? "." : ",");
if (attemptedValue.IndexOf(wantedSeperator) == -1
&& attemptedValue.IndexOf(alternateSeperator) != -1)
{
attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator);
}
// If SetModelValue is not called it may result in a null-ref exception if the model is resused - c.
bindingContext.ModelState.SetModelValue(modelName, bindingContext.ValueProvider[modelName]);
try
{
// Added support for decimals and nullable types - c.
if (bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(double?))
{
result = double.Parse(attemptedValue, NumberStyles.Any);
}
else
{
result = decimal.Parse(attemptedValue, NumberStyles.Any);
}
}
catch (FormatException e)
{
bindingContext.ModelState.AddModelError(modelName, e);
}
}
else
{
result = base.BindModel(controllerContext, bindingContext);
}
return result;
}
}
それはきれいではありませんが、それ働く
関連する問題
- 1. 通貨などのフォーマットdouble値が、通貨記号(C#の)
- 2. フォーマット通貨
- 3. 通貨フォーマット
- 4. asp.netラベル通貨フォーマット
- 5. 通貨フォーマットはC#
- 6. 国際通貨のフォーマット
- 7. WinForms通貨テキストボックスのフォーマット
- 8. スプレッドシートの宝石を使用した通貨番号のフォーマット
- 9. Javaの文字列/数値/通貨フォーマット機能
- 10. フォーマット通貨in特定の国ではobj c
- 11. 小数点を通貨にフォーマットし、$ 0〜$ 1の値をセントで表示する
- 12. フォーマット10進数からインド通貨へのフォーマット
- 13. Combobox値通貨へのメンバー
- 14. JavaScriptを使用して通貨をフォーマットする
- 15. 10進数を通貨としてフォーマットする方法。
- 16. Railsの - 通貨形式としてフォーマット番号ゲッターで
- 17. 通貨の値をNAN、INFまたは-INFに設定しますか?
- 18. 通貨リストアイテムの値を取得する
- 19. ロケールがフランス語に設定されている場合のBlackBerryの通貨フォーマット
- 20. Sql Serverデータベースのデータを使用して通貨(US)をフォーマットしない
- 21. SELECT文で通貨と小数点をフォーマットする方法
- 22. Virtuemartの各通貨の定価
- 23. VS2010:通貨のテキストボックスの書式設定
- 24. Excelセル(通貨)の書式設定
- 25. 数値または通貨の文字列形式ですか?
- 26. 数値(非通貨)の値をテキストエントリに変換する
- 27. 数値をモデル内の通貨に変換しますか?
- 28. 通貨の値を四捨五入して格納する
- 29. 通貨グローバル化jsを使用して異なる国でフォーマットする
- 30. 通貨マスク
私はスタックを困惑しましたか?それはそれほど難しいはずはないようですね? –