How to save information(Cpf) from an Edit with mask and a numeric field

Asked

Viewed 148 times

0

In the bank 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’ve been using a Datatype.Text to accept the mask, but when I leave the field (Onblur) the following error message appears, I’m not the one who put it and I don’t know where it comes from:

The CPF field must be a number

That’s my model, but I’ll take a 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 mask jquery and a js function to try to pass just the number, but I understand that even without the mask signals, Cpf remains a string and not a number. Down with functions

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="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 quite simple: Is there any way I can create an edittext with mask and then while recording, move from text to long and record?

  • 1

    I believe that it would be a good practice to separate the bank model from the display model. If you had a field for example "ZIP code" in the bank of the type int but wanted in its View turn him into two inputs, how would you do it? So it’s best to have a model to the View and another that is your data model

  • @Ricardopunctual, explain something to me. I have an API and a MVC(View). If I keep the API model as a long and in MVC would I do a String(Model)? That’s it?

  • Yes, and of course when you are saving information you will need to pass the data from one model to the other, on controller for example, but this gives you more flexibility and does not cause dependencies between the template and what is on the page. All those attributes of Annotations, as requred and display do not need to be in your data model model, only in the model you use in view

  • So you say I create a Viewmodel. I saw it now with a colleague and I think that’s the way.

  • Exactly, a model to suit the View. Hence you can set the property as string, format, leave it the way it suits your View well.

  • I created a Viewmodel, but at the time of passing the CPF is going 0.0. There is something missing in this conversion that I am not being able to do, a javascript function and so on. How do I move from the VM string to my service to receive decimal (11.0)? I switched from bigint to decimal, because the requirement is numeric to 11 and only decimal allows me a length of the field

Show 1 more comment

1 answer

0

Ideal would be to create a Viewmodel mapper for the Model, where you should convert from the service layer call inside the controller.

A suggestion is to use the library Automapper

Maybe this answer will help with the use of Automapper: Automapper and Viewmodel with equal fields

  • I have an API that was matching with the model. I created the VM and as the API is, that’s the question. Automapper solves this?

  • In reality it is just a field, which in the VM is string, but the API should take this field as decimal(11.0). It will need an Automapper even?

  • you can convert the VM when falling into the controller, face is the easiest way to convert. As ta your controller ?

Browser other questions tagged

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