ASP.NET MVC

  • ASP.NET MVC custom binder for currency

    I can imagine that’s quite common problem. You have a double (or better decimal) value that you want to show formatted as a currency field. Lets assume we are storing the price data in an object like this: public class TestModel { public double NumberField { get; set; } public double CurrencyField { get; set; } } The MVC view is strongly typed with this TestModel. And the view model value is formatted like this: <%=Html.TextBox(“NumberField”, Model.NumberField)%> <%=Html.TextBox(“CurrencyField”, Model.CurrencyField.ToString(“c”))%> If you set the current culture to German-Swiss, for example in (base)controller like that: protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); string culture = "de-CH"; CultureInfo ci = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture =…