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?
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 twoinputs
, how would you do it? So it’s best to have a model to the View and another that is your data model– Ricardo Pontual
@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?
– pnet
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
– Ricardo Pontual
So you say I create a Viewmodel. I saw it now with a colleague and I think that’s the way.
– pnet
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.– Ricardo Pontual
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
– pnet