C # decimal field in Form

1

Simple question:

I have a Decimal field of type Currency in my model. It happens that when I enter a decimal value in the form field, it arrives as integer in the Controller:

Example:

KM Value Rounded: 0.41 Arrives on Controller: 41

My Model:

[Required]
    [Display(Name = "KM")]
    [DataType(DataType.Currency, ErrorMessage = "Valor deve ser uma quantia de dinheiro válida")]
    [Range(0.01, 9999.00)]
    public double ValorKM { get; set; }

My Controller:

ModelState.Remove("URLFoto");
        if (ModelState.IsValid)
        {
            db.Entry(frota).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

My Form:

<div class="col-md-12">
        <div class="col-md-2">@Html.LabelFor(model => model.ValorKM)</div>
        <div class="col-md-2">
            @Html.TextBoxFor(model => model.ValorKM, new { type = "text" })
            @Html.ValidationMessageFor(model => model.ValorKM)
        </div>
    </div>

Another question is how do I change my decimalSeparator. Change the "." by ","?

    
asked by anonymous 18.06.2015 / 03:34

1 answer

1

Well, first I recommend using decimal instead of double in:

public decimal ValorKM { get; set; }

And remove this dataType currency , so it makes your work much easier.

Second, in view , I recommend using a plug-in formatting like jQuery Mask Plugin .

    
18.06.2015 / 03:40