remove mask to save to the database

1

I'm using MaskMoney to create a mask in the field for the user to type. wanted to remove the mask, saving only the numbers and the comma (2 decimal places) in the bank.

  

$ 1,300,000.00

Just saving

  

1300000.00

I'm using asp.net-mvc, follow the controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "PessoaID,Nome,Telefone,Celular,Whatsapp,Email,CPF,Tipo,RG,CTPS,DataNascimento,DataAdmissao,DataDemissao,Senha,CargoID")] Funcionario funcionario)
    {
        //salario = Convert.ToString(salario);
        salario = Regex.Replace(salario, "[^0-9,]", "");

        funcionario.Salario = Convert.ToDecimal(salario);
        funcionario.tipo = 1;
        if (ModelState.IsValid)
        {
            db.Pessoas.Add(funcionario);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.CargoID = new SelectList(db.Cargos, "CargoID", "Descricao", funcionario.CargoID);
        return View(funcionario);
    }

Create:

@model SAQ.Models.Funcionario

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4 onmouseover="tooltip.pop(this, 'Este é meu texto na tooltip.')">Funcionario</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Telefone, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Telefone, new { htmlAttributes = new { @class = "form-control telefone" } })
            @Html.ValidationMessageFor(model => model.Telefone, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Celular, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Celular, new { htmlAttributes = new { @class = "form-control celular", id = "txt-tel" } })
            @Html.ValidationMessageFor(model => model.Celular, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Whatsapp, htmlAttributes: new { @class = "control-label col-md-2", id = "bt-copiar", title = "Clique em mim para copiar o Celular" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Whatsapp, new { htmlAttributes = new { @class = "form-control celular", id = "txt-wpp", title = "Clique no Texto WhatsApp ao lado para copiar o Celular" } })
            @*<div class="btn" id="bt-copiar">Copiar</div>*@
            @Html.ValidationMessageFor(model => model.Whatsapp, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CPF, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CPF, new { htmlAttributes = new { @class = "form-control cpf" } })
            @Html.ValidationMessageFor(model => model.CPF, "", new { @class = "text-danger" })
        </div>
    </div>

    @*<div class="form-group">
        @Html.LabelFor(model => model.tipo, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.tipo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.tipo, "", new { @class = "text-danger" })
        </div>
    </div>*@

    <div class="form-group">
        @Html.LabelFor(model => model.RG, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RG, new { htmlAttributes = new { @class = "form-control rg" } })
            @Html.ValidationMessageFor(model => model.RG, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CTPS, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CTPS, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CTPS, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { @class = "form-control data" } })
            @Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DataAdmissao, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DataAdmissao, new { htmlAttributes = new { @class = "form-control data" } })
            @Html.ValidationMessageFor(model => model.DataAdmissao, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Salario, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Salario, new { htmlAttributes = new { @class = "form-control " } })
            @Html.ValidationMessageFor(model => model.Salario, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control", id="Senha"} })
            @Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        @Html.LabelFor(model => model.CargoID, "Cargo", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CargoID", null, "Selecione", htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CargoID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Salvar" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
<script type="text/javascript">
        $(document).ready(function () {
            $(".telefone").inputmask("mask", { "mask": "(99)9999-9999" });
            $(".celular").inputmask("mask", { "mask": "(99)99999-9999" });
            $(".cpf").inputmask("mask", { "mask": "999.999.999-99" }, { reverse: true });
            $(".data").inputmask("mask", { "mask": "99/99/9999" });
            $(".rg").inputmask("mask", { "mask": "99.999.999-9" });
            $(".dinheiro").maskMoney({ prefix: 'R$ ', allowNegative: true, thousands: '.', decimal: ',', affixesStay: true });
        });
</script>
<script> //copia os caracteres do telefone
    $('#bt-copiar').on('click', function () {
        $('#txt-wpp').val($('#txt-tel').val());
    });
</script>
    <script>
        $(document).ready(function (e) {
            $('#Senha').on('focus', function () {
                $(this).attr('type', 'text');
            }).on('blur', function () {
                $(this).attr('type', 'password');
            });
        });
    </script>

}

class (I'm just putting the attribute that gives me the problem):

[DisplayFormat(DataFormatString = "{0:c}")]
[Required(ErrorMessage = "Preencha o Salário")]
[DisplayName("Salário")]
public decimal Salario { get; set; }
    
asked by anonymous 13.11.2016 / 00:55

2 answers

4

I solved my problem like this:

public ActionResult Create([Bind(Include = "PessoaID,Nome,Telefone,Celular,Whatsapp,Email,CPF,Tipo,RG,CTPS,DataNascimento,DataAdmissao,DataDemissao,Senha,CargoID")] Funcionario funcionario, string salario)
{
        salario = Regex.Replace(salario, "[^0-9,]", "");
        funcionario.Salario = Convert.ToDecimal(salario);
}

That is, it includes a salary string, and I dealt with Regex.Replace then saved normally.

    
13.11.2016 / 02:09
2

The decimal data types are saved in the database with the format, ie, ########.## , hence the string of the example in the format $ 1,300,000, 00 has to be passed to the database with the format 1300000.00 .

As already mentioned you could use JavaScript for this, but there is no guarantee of security there.

Using C # you could easily implement a method to replace all non-numeric characters of string and finally convert it to decimal :

public decimal Salario
{
    get { return _salario; }
    set { _salario = value.Replace("R$ ", string.Empty).Replace(".", string.Empty).Replace(",", "."); }
}

This is just an example. You could use various functions to treat string .

Microsoft recommends using .tryParse () .

You could still use

var _salario = string.Join("", _salario.Where(char.IsDigit));

_salario = _salario/100;

The above example joins together all numeric characters in a single string , so you have to divide by 100 , since R$ 1.300.000,00 would return 130000000 .

    
13.11.2016 / 02:00