Dos campos con el mismo nombre en la carpeta de modelos personalizados

I write a custom model binder. In my page I have a numeric text box that has comma separetor mask. For examle:

1,234

In my model binder, I get value in text box:

var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName)

and remove "," from it:

actualValue = long.Parse(a, NumberStyles.Currency, CultureInfo.CurrentCulture);

Everything is OK until in another page I have two field with same name. When I submit form, one field has value "0" and one field has another value (for examlpe "1"). Then when I get valueResult from :

var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName)

It returns me: "1,0" and then when I remove "," from it, It will be "10".

But in default mvc model binder, when I have two field with same name, It get first field's value.

How can I do something like default mvc model binder?

Gracias.

preguntado el 28 de mayo de 14 a las 14:05

Show your model code. -

1 Respuestas

I don't understand so much about custom model binders... but if you want to bind your numbers inputs (using comma as decimal separator) to decimal, just change de culture of your thread or change of the entire application.

Por ejemplo:

public class MyModel
{
      public decimal MyValue { get; set; }
}

Now go to your web.config, inside system.web (I'm from Brazil, so in my case I used pt-BR culture, you should change to your culture):

<globalization culture="pt-BR" />

Now, the MVC Model Binder will automatically recognize "comma" as decimal separator, everything should works well.

I'm not sure if it helps you.

contestado el 28 de mayo de 14 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.