correctly obtain decimal values in controller MVC

0

I created a view based on a model using MVC C # in this model I have a property for the cost of an event, which according to my country can contain the following valid values (separated by hyphen): $ 1,234.56 - $ 123,123 , 54 - $ 1.25 and the property with validations using data annotations is:

 [Required(ErrorMessage="Especifique precio evento")]
        [Display(Name="Precio Evento")]
        [DisplayFormat(DataFormatString = "{0:C0}")]      
        [RegularExpression(@"^(((\d{1,3})(,\d{3})*)|(\d+))(.\d+)?$", ErrorMessage = "Error")]

        public double PrecioEvento { get; set; }

my problem is that when I send to my form, in the controller I receive these values 1.25 - 25.47 - 789.32 of PriceEvent ... but when the value specified for PriceEvent takes a comma for example 123,123.45 I receive 0.0 in my controller.

at the beginning every time I wrote a number with a decimal point I received zero in my controller but I fixed it by adding this to my web.config

<globalization culture="es-SV"/> 

but now I do not know what I should modify to get in my controller values specified in my view that have a comma (thousands) and a decimal point, for example: 123,123.45.

Thanks for the help, Note: the regular expression I use works to get the desired values (comma of thousands and decimal point).

    
asked by 23.03.2017 в 20:49
source

1 answer

1

In your case the error is that by the comma the value is receive is a string when your variable PriceEvent is double

I recommend you use pre-designed formats. This is an example:

  String.Format("{0:#,0.000}", value)

You can apply it from your model

public double PrecioEvento { get; set; }
public double strPrecioEvento { get
    {
        return String.Format("{0:#,0.000}", PrecioEvento );
    }
    set
    {
        PrecioEvento = value;
    } 
}

and use strPrecioEvento in your view instead of PriceEvento

    
answered by 23.03.2017 в 21:04