1

This is probably a dummy question, but I'm stuck with a problem I can't resolve by myself.

I'm working on an ASP.NET MVC3 project. For some reason, on a particular Create page, a decimal field is not binded on the model when the page is validated and I can't understand why.
Here is my code :

@model ModelA

/*some TextBoxFor or others DropDownListFor, all of them working well*/

@Html.TextBoxFor(m => m.ModelB.ModelC.MyField, new { @id = "txtMyField" })

When I validate the page, the model ModelA is correctly filled EXCEPT for this particular field MyField. Others fields are OK, for exemple :

@Html.TextBoxFor(m => m.ModelB.AnotherField, new { @id = "txtAnotherField" })

perfecly works, and others properties of ModelB are correctly filled.

My models

ModelA contains a property of type ModelB called ModelB.
ModelB contains a property of type ModelC called ModelC.
ModelC contains a property called MyField. (I'll probably add one or two more in the future). This field is a decimal field, without any DataAnnotation or anything.

public decimal MyField { get; set; }


By the way, when in my browser's console I request

$('#txtMyField').val()

it displays the good value, for example 12.54.


What I tried

  • Change this decimal field to a float type, the problem is the same.
  • Put it in an EditorFor instead of a TextBoxFor, the problem is the same.
  • Put the field MyField in ModelB. The only ModelB's property to 0 is MyField.


Why is this field a rebel ? I probably do something wrong, but I can't understand where... Can someone explain me where is my mistake ?
Thank you !

2
  • Have you tried renaming the property? Just to discard for example a collision with a route parameter Commented Jan 18, 2014 at 18:13
  • I tried it but it doesn't help Commented Jan 18, 2014 at 19:22

1 Answer 1

2

Ok, so here is the solution :

A "correct" decimal value looks like 45.68 with a . as a separator.
But the application is a non-English application, and the separator need to be a ,. By default, 45,68 is not a correct number. So I added this piece of code :

jQuery.extend(jQuery.validator.methods, {
    number: function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});

and 45,68 is recognized as a correct number.

Sign up to request clarification or add additional context in comments.

1 Comment

Easiest & best way to solve the problem ! THANK YOU A LOT !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.