Mask with ASP.NET Core MVC

0

I'm doing a job for college with asp.net core MVC , and I'm using entity to generate the view and queries in the BD. I need to put masks in the fields like CNPJ. I got searching on Google how to put mascara, but when I try to create a record, it gives the error because of the mask.

I would like to know how to remove the mask and send the numbers to the DB.

View:

<div class="col-md-4">
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="RazaoSocial" class="control-label"></label>
            <input asp-for="RazaoSocial" placeholder="Razão Social" class="form-control" , />
            <span asp-validation-for="RazaoSocial" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="NomeFantasia" class="control-label"></label>
            <input asp-for="NomeFantasia" placeholder="Nome Fantasia" class="form-control" />
            <span asp-validation-for="NomeFantasia" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Cnpj" class="control-label"></label>
            <input asp-for="Cnpj" placeholder="Somente Numero" data-mask="00.000.000/0000-00" data-mask-reverser="false" class="form-control" />
            <span asp-validation-for="Cnpj" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Ie" class="control-label"></label>
            <input asp-for="Ie" placeholder="Somente Numero" class="form-control" />
            <span asp-validation-for="Ie" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Avançar" class="btn btn-default" />
        </div>
    </form>
</div>

Controller

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("EmpresaId,RazaoSocial,NomeFantasia,Cnpj,Ie")] Empresa empresa)
    {

        if (ModelState.IsValid)
        {

            _context.Add(empresa);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(empresa);
    }
    
asked by anonymous 20.03.2018 / 02:57

1 answer

0

You can clear the CNPJ field by removing the special characters before sending to the database. To do this, create a method for this. Add the following method to your Controller :

private string ClearCNPJ(string cnpj)
{
   return cnpj.Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty);
}

To use, within the Create method, type as follows:

if (ModelState.IsValid)
{
    empresa.Cnpj = ClearCNPJ(empresa.Cnpj);
    _context.Add(empresa);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}

return View(empresa);    

Or you can even create an extension method that can be used on any% created%. Create a class string equal to the example below.

 public static class StringExtensions
 {
     public static string ClearCNPJ(this string value)
     {
          return value.Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty);
     }
 }

In the class you will use ( static in your case), put the Controller directive:

using seuprojeto.StringExtensions;

And to use, within the using method, put it as follows:

if (ModelState.IsValid)
{
    empresa.Cnpj = empresa.Cnpj.ClearCNPJ();
    _context.Add(empresa);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}

return View(empresa);   
    
20.03.2018 / 07:43