<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kristof Neirynck &#187; ASP.NET MVC</title>
	<atom:link href="http://www.crydust.be/blog/category/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.crydust.be/blog</link>
	<description>yet another webdev blog</description>
	<lastBuildDate>Wed, 05 May 2010 16:13:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Custom model binder to avoid decimal separator problems</title>
		<link>http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/</link>
		<comments>http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 10:52:55 +0000</pubDate>
		<dc:creator>Kristof Neirynck</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.crydust.be/blog/?p=116</guid>
		<description><![CDATA[I bumped into an issue while trying to get the open source ASP.NET MVC project NerdDinner running on my laptop. The Create action in the DinnersController just wouldn&#8217;t work. This was due to Latitude and Longitude values being set with a different decimal separator than my server expected. It got &#34;0.0&#34; instead of &#34;0,0&#34; which [...]]]></description>
			<content:encoded><![CDATA[<p>I bumped into an issue while trying to get the open source ASP.NET MVC project <a href="http://nerddinner.codeplex.com/">NerdDinner</a> running on my laptop. The <code>Create</code> action in the <code>DinnersController</code> just wouldn&#8217;t work. This was due to <code>Latitude</code> and <code>Longitude</code> values being set with a different decimal separator than my server expected. It got &quot;0.0&quot; instead of &quot;0,0&quot; which the <code>Double.parse</code> function didn&#8217;t understand. A client-side JavaScript wrote these values into hidden fields, so it was a bit difficult for me to understand at first. I wrote a custom model binder to solve this for all fields of type <code>double</code> at once.</p>
<p>As I&#8217;m still learning ASP.NET MVC, my solution may not be the optimal one.</p>

<span id="more-116"></span>

<p>Create a custom model binder that will only react differently to <code>double</code> values.</p>
<pre class="prettyprint">
using System;
using System.Globalization;
using System.Web.Mvc;

namespace NerdDinner.Helpers {
  public class CustomModelBinder : DefaultModelBinder {

    public CustomModelBinder()
      : base() {
    }

    public override object BindModel(ControllerContext controllerContext, 
      ModelBindingContext bindingContext) {
      
      object result = null;

      // Don't do this here!
      // It might do bindingContext.ModelState.AddModelError
      // and there is no RemoveModelError!
      // 
      // result = base.BindModel(controllerContext, bindingContext);

      if (bindingContext.ModelType == typeof(double)) {

        string modelName = bindingContext.ModelName;
        string attemptedValue = bindingContext.ValueProvider[modelName].AttemptedValue;

        // Depending on cultureinfo the NumberDecimalSeparator can be &quot;,&quot; or &quot;.&quot;
        // Both &quot;.&quot; and &quot;,&quot; should be accepted, but aren't.
        string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
        string alternateSeperator = (wantedSeperator == &quot;,&quot; ? &quot;.&quot; : &quot;,&quot;);

        if (attemptedValue.IndexOf(wantedSeperator) == -1 
          &amp;&amp; attemptedValue.IndexOf(alternateSeperator) != -1) {
          attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator);
        }

        try {
          result = double.Parse(attemptedValue, NumberStyles.Any);
        }
        catch (FormatException e) {
          bindingContext.ModelState.AddModelError(modelName, e);
        }

      }
      else {
        result = base.BindModel(controllerContext, bindingContext);
      }

      return result;
    }
  }
}
</pre>

<p>Change the <code>DefaultBinder</code> in <code>Global.asax</code>.</p>
<pre class="prettyprint">
void Application_Start() {
  //[SNIP]
  ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
