How to remove mascara from the field before calling the controller with asp net MVC

4

In my registration form I have a CNPJ field with the mask set via jquery $("#cnpj").mask("99.999.999/9999-99"); however the value is arriving in my formatted controller, in my database this field is float, is there a way to handle this without turning the field into string and performing the replace?

    
asked by anonymous 04.11.2015 / 00:47

2 answers

1

Hello, how are you? A valid alternative is to put the input for your model as hidden:

@Html.HiddenFor(model => model.Cnpj)

And then change the value of this field when filling your field with mascara, let's call it cnpj-with-mask, ok?

It would look like this:

var options =  { 
  onChange: function(cnpj){
    $("#cnpj").val(cnpj);
  }
};

$('#cnpj-with-mask').mask('99.999.999/9999-99', options);
    
04.11.2015 / 22:05
0

The form I found to resolve is based on a string field not mapped in Model and another decimal , this yes mapped:

    [Required]
    public Decimal Cnpj { get; set; }

    [NotMapped]
    [Cnpj] 
    public String CnpjMascara { get; set; }

I do not recommend using float for CNPJ because float is a structure that works well with numbers that have mantissa and feature, which is not the case with CNPJ.

The Attribute for CNPJ validation is below:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class CnpjAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null) return null;

        int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
        int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };

        int soma, resto;

        string digito, tempCnpj, CNPJ;

        CNPJ = value.ToString().Trim();
        CNPJ = CNPJ.Replace(".", "").Replace("-", "").Replace("/", "");

        if (CNPJ.Length != 14)
            return new ValidationResult("CNPJ Inválido.");

        tempCnpj = CNPJ.Substring(0, 12);
        soma = 0;

        for (int i = 0; i < 12; i++)
            soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];
        resto = (soma % 11);

        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = resto.ToString();
        tempCnpj = tempCnpj + digito;
        soma = 0;

        for (int i = 0; i < 13; i++)
            soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];
        resto = (soma % 11);

        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = digito + resto.ToString();

        if (CNPJ.EndsWith(digito))
            return null;
        else
            return new ValidationResult("CNPJ Inválido.");
    }

    public override string FormatErrorMessage(string name)
    {
        return name;
    }
}

Since CnpjMascara is already validated by the attribute, you only need to assign it to the corresponding type before passing ModelState :

    [HttpPost]
    public ActionResult Create(Empresa empresa)
    {
        if (empresa.CnpjMascara != null)
            empresa.Cnpj = Decimal.Parse(empresa.CnpjMascara.Replace(".", "").Replace("-", ""));

        if (ModelState.IsValid)
        {
           /* Coloque aqui a lógica padrão */
        }
    }
    
04.11.2015 / 23:27