Decimals in an ASP.NET MVC 5 Application

I have a problem with decimal numbers.

If I use. (dot) instead of, (comma) in a text field, it is null in the controller.

I know his problem in the language, because in Spanish we use a comma instead of a period for decimal places, but I need to use a period.

Is it possible to change this?

This is strange because in the controller I have to use. (dot) for decimal points ie:

I can do it float x = 3.14, but I can’t do it float x = 3,14, so I don’t understand this ... In some cases I have to use a period ... In others I have to use a comma ...

This is my code:

In the model:

[Display(Name = "Total")]
public double Total { get; set; }

In sight:

@Html.EditorFor(model => model.Total, new { id = "Total", htmlAttributes = new {@class = "form-control" } })

In the controller:

public ActionResult Create([Bind(Include = "ID,Codigo,Fecha,Trabajo,Notas,BaseImponible,Iva,Total,Verificado,FormaDePagoID,ClienteID")] Presupuesto presupuesto)
    {
+4
source share
4

. Phil Haack, .

public class ModelBinder
{
    public class DecimalModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext,
                                         ModelBindingContext bindingContext)
        {
            object result = null;

            // Don't do this here!
            // It might do bindingContext.ModelState.AddModelError
            // and there is no RemoveModelError!
            // 
            // result = base.BindModel(controllerContext, bindingContext);

            string modelName = bindingContext.ModelName;
            string attemptedValue =
                bindingContext.ValueProvider.GetValue(modelName).AttemptedValue;

            // Depending on CultureInfo, the NumberDecimalSeparator can be "," or "."
            // Both "." and "," should be accepted, but aren't.
            string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
            string alternateSeperator = (wantedSeperator == "," ? "." : ",");

            if (attemptedValue.IndexOf(wantedSeperator) == -1
                && attemptedValue.IndexOf(alternateSeperator) != -1)
            {
                attemptedValue =
                    attemptedValue.Replace(alternateSeperator, wantedSeperator);
            }

            try
            {
                if (bindingContext.ModelMetadata.IsNullableValueType
                    && string.IsNullOrWhiteSpace(attemptedValue))
                {
                    return null;
                }

                result = decimal.Parse(attemptedValue, NumberStyles.Any);
            }
            catch (FormatException e)
            {
                bindingContext.ModelState.AddModelError(modelName, e);
            }

            return result;
        }
    }
}

Application_Start() Global.asax

    ModelBinders.Binders.Add(typeof(decimal), new ModelBinder.DecimalModelBinder());
    ModelBinders.Binders.Add(typeof(decimal?), new ModelBinder.DecimalModelBinder());

float double, ! , , !!

+6

#. , . - . . , .

( ) , # (US). , .

0

, (,) , (.) #, Asp.Net MVC, , # decimal.

The advantage is that it is reused in the application, where you may have repeated scripts for decimal conversions.

The hopes of the following links may help you:

ASP.Net MVC Custom Model Binding Description http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx http: // haacked .com / archive / 2011/03/19 / fixing-binding-to-decimals.aspx /

0
source

All Articles