How to write information (cpf) of an edit with a mask and a numeric field

0

In the database I have the CPF field of type bigint. In the model it is a long. This is what was requested in the requirement. Well, in the model I will masquerade for a DataType.Text to accept the mask, but when I leave the field (OnBlur) the following error message appears, that I was not who put it and I do not know where it comes from:

  

The CPF field must be a number

This is my model, but I accept suggestion

public class Funcionario

{
    [Key]
    public int id { get; set; }

    [Required(ErrorMessage ="Nome do funcionário é obrigatório", AllowEmptyStrings =false)]
    [Display(Name ="Nome")]
    public String nome { get; set; }

    [Required(ErrorMessage = "Data de Nascimento do funcionário é obrigatório", AllowEmptyStrings = false)]
    [Display(Name = "Data de Nascimento")]
    [DataType(DataType.Date, ErrorMessage = "formato de data invalido")]
    public DateTime dataNascimento { get; set; }

    [Required(ErrorMessage = "CPF do funcionário é obrigatório", AllowEmptyStrings = false)]
    [Display(Name = "CPF")]
    [DataType(DataType.Text, ErrorMessage ="Formato inválido")]
    public long cpf { get; set; }

    [Display(Name = "Nome da Cidade")]
    public String NomeCidade { get; set; }

    [Required(ErrorMessage = "Cidade do funcionário é obrigatório", AllowEmptyStrings = false)]
    [Display(Name = "Cidade")]
    public virtual int cidade { get; set; }
}

In my cshtml, I created a jquery to mask and a js function to try to pass only the number, but I understand that even without the mask's signals, the cpf is still a string and not a number. Below the functions

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://rawgit.com/RobinHerbots/Inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>

    <script>
        $("#cpf").inputmask("mask", {
            "mask": "999.999.999-99"
        }, {
                reverse: true
            });
        $("#nascimento").inputmask("mask", {
            "mask": "99/99/9999"
        }, {
                reverse: true
            });


        function replaceCpf() {
            var campo = document.getElementById('cpf');
            var cpf = campo.value;
            campo.value = cpf.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})/, "$1.$2.$3-$4");
        }

    </script>

The question is very simple: Is there any way I can create an edittext with a mask and then when I save, move from text to long and save?

    
asked by anonymous 12.08.2018 / 18:28

1 answer

0

The ideal would be to create a ViewModel mapper for the Model, where it should convert from the service layer call into the controller.

One suggestion is to use the AutoMapper library

Maybe this answer will help with the use of AutoMapper: AutoMapper and ViewModel with Fields equal

    
13.08.2018 / 04:25