0

I have a complex type

public class Model 
{
    public double D2 { get; set; }
}

And I receive this type inside action

[HttpPost("{id}")]
public string Post(Model model)
{
    return "";
}

Users want to have ability to type numbers with . (point) and , (comma) decimal separators, for example 234.245 and 234,245 are valid decimal types. But framework can't convert this numbers correctly. Is there a way to "catch" the binding for doubles to have ability to normalize symbols before converting to double?

Wachburn
  • 2,842
  • 5
  • 36
  • 59
  • not sure if this works in `.net-core` http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/ – adiga Sep 08 '17 at 14:22
  • You always can write custom ModelBinder, but I sovle same situation by replacing commas by dots on client side – Anton Sutarmin Sep 08 '17 at 14:23
  • Possible duplicate of [How to set decimal separators in ASP.NET MVC controllers?](https://stackoverflow.com/questions/793459/how-to-set-decimal-separators-in-asp-net-mvc-controllers/5117441). – Win Sep 08 '17 at 14:28

1 Answers1

0

Sure, there is a way. Grab d2 as string and then replace . to , and convert into double or whatever you want. For example:

public string Post(string d2)
{
    var model = new Model(){ D2 = Convert.ToDouble(d2.Replace(".", ",")) } 
    return "";
}

If you don't like this way make custom setter in Model by almost the same way

J. Doe
  • 2,651
  • 1
  • 13
  • 31