C# decimal field in the form

Asked

Viewed 1,626 times

1

Simple question:

I have a currency decimal field on my model. It happens that when I insert a decimal value in the form field, it arrives as integer in the Controller:

Example:

Value of KM Wheelset: 0.41 Arrives in 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. Exchange the "." for ","?

1 answer

1

Good, first recommend using decimal instead of double in:

public decimal ValorKM { get; set; }

And remove this one dataType currency, thus facilitates and greatly your work.

Second, in the view, recommend you use a plug-in format as the jQuery Mask Plugin.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.