0

I need to implement a currency input where the property type needs to be decimal or double and accept the format "0.00,00".

ViewModel

[RegularExpression(@"^([1-9]{1}[\d]{0,2}(\.[\d]{3})*(\,[\d]{0,2})?|[1-9]{1}[\d]{0,}(\,[\d]{0,2})?|0(\,[\d]{0,2})?|(\,[\d]{1,2})?)$")]
public Decimal Salario { get; set; }

EditorTemplate

@model Decimal?

@Html.TextBox("", Model.HasValue && Model.Value > 0 ? Model.Value.ToString() : "", new { @class = "form-control text-box single-line money" })


<script type="text/javascript">
    $(document).ready(function () {
        $(".money").maskMoney({ thousands: '.', decimal: ',', prefix: "" });
    });
</script>

I also have set on my web.config culture="pt-BR". The problem is that the input is only accepting values like "123,98", if i type "1.123,98" I get the error message "The value '9.999,99' is not valid for Salario.". Is there a way to make it allows the dots and comma? I don't want to use System.String.

UPDATE - The solution

I finally found a solution! This is my final code:

EditorTemplate

@model Double?

@Html.TextBox("", Model.HasValue && Model.Value > 0 ? Model.Value.ToString() : "", new { @class = "form-control text-box single-line money" })


<script type="text/javascript">
    $(document).ready(function () {
        $(".money").maskMoney({ thousands: '.', decimal: ',', prefix: "R$ "     });
    });
</script>

ViewModel

[DataType("Money")] //Money is the name of my EditorTemplate
public decimal Valor { get; set; }

Custom Model Binder

public class DoubleModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        return valueProviderResult != null && !string.IsNullOrEmpty(valueProviderResult.AttemptedValue) ? Convert.ToDouble(valueProviderResult.AttemptedValue.Replace("R$", "").Trim()) : base.BindModel(controllerContext, bindingContext);
    }
}

Global.asax

ModelBinders.Binders.Add(typeof(double), new DoubleModelBinder());
ModelBinders.Binders.Add(typeof(double?), new DoubleModelBinder());

This custom model binder is used to format the value before it is sent to controller. I found the solution on this post: Accept comma and dot as decimal separator.

Thanks!

Community
  • 1
  • 1
Deise Vicentin
  • 137
  • 1
  • 4
  • 23
  • Scripts should never be in an `EditorTemplate` - move that to the main view. Are you using unobtrusive client side validation (i.e. getting a validation error that prevents your form submitting - in which case you need to reconfigure the `$.validator`), or is the validation occurring on the server (in which case, what is your server culture) –  May 18 '16 at 22:23
  • Just as a comment, you should probably be storing currencies in the smallest sub unit as an `int`. – Tom Gullen May 18 '16 at 22:38

1 Answers1

0

This has been answered before have a look at Currency Formatting MVC that Darin Dimitrov had provided. The key is the DataAnnotation's for the field using the DisplayFormat annotation

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
Community
  • 1
  • 1
Mark
  • 2,454
  • 4
  • 26
  • 29
  • If you think its a duplicate, then flag it as such. But this has nothing to do with OP's issue anyway (which is to do with validation) –  May 18 '16 at 23:20